Regular Expressions 101

Community Patterns

Regex to match any JSON value with fewer steps possible

1

Regular Expression
PCRE (PHP <7.3)

/
(?smx) (?(DEFINE) (?<jsonObject> \{(?&jsonField)(?>,(?&jsonField))*+\}| \{[[:space:]]*+\} ) (?<jsonArray> \[(?&jsonValue)(?>,(?&jsonValue))*+\]| \[[[:space:]]*+\] ) (?<string> " [^"\\]*+ (?> (?> \\["\\\/rntfb]| \\u[[:xdigit:]]{4} )++ [^"\\]*+ )*+ " ) (?<number> \-?+ (?> [1-9][[:digit:]]*+| 0 ) (?>\.[[:digit:]]++)?+ (?>[eE][\+\-]?+[[:digit:]]++)?+ ) (?<jsonValue> [[:space:]]*+ (?> (?&string)| (?&number)| (?&jsonObject)| (?&jsonArray)| false| true| null ) [[:space:]]*+ ) (?<jsonField> [[:space:]]*+(?&string)[[:space:]]*+\: (?&jsonValue) ) ) ^(?&jsonValue),\n
/
g

Description

If you find...

  • any JSON value that should be matched but isn't,
  • any JSON value that should NOT be matched but is
  • or simply a way to match the values with fewer steps... please, let me know.
Submitted by anonymous - 5 years ago