Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE (PHP <7.3)

/
(?(DEFINE) # JSON does not need backtracking if it's valid # so everything is atomic to prevent backtracking # regex is built per https://tools.ietf.org/html/rfc8259 (?<json>(?>\s*(?&object)\s*|\s*(?&array)\s*)) (?<object>(?>\{\s*(?>(?&member)(?>\s*,\s*(?&member))*)?\s*\})) (?<member>(?>(?&string)\s*:\s*(?&value))) (?<array>(?>\[\s*(?>(?&value)(?>\s*,\s*(?&value))*)?\s*\])) (?<value>(?>)false|null|true|(?&object)|(?&array)|(?&number)|(?&string)) (?<number>(?>-?(?>0|[1-9][0-9]*)(?>\.[0-9]+)?(?>[eE][-+]?[0-9]+)?)) (?<string>(?>"(?>\\(?>["\\\/bfnrt]|u[a-fA-F0-9]{4})|[^"\\\0-\x1F\x7F]+)*")) ) \A(?&json)\z
/
x

Description

PCRE Regular expression that validates JSON according to grammar specified at RFC-8259 (https://tools.ietf.org/html/rfc8259)

Submitted by anonymous - 5 years ago