Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE (PHP <7.3)

/
[:,\{\}\[\]]|(\".*?\")|('.*?')|[-\w.]+
/
g

Description

I figured this would be the simplest way to Parse JSON. We have some known information about the properties to help determine the type at runtime.

  • Properties - Immediately followed by : characters
  • Values - Have a bracket } ] or a , directly after it in the regex return.
  • Strings - They are surrounded by " " and also can't adhere to Properties requirements.
  • Numbers - They have no " " surrounding them and only contain characters [0-9]. Can't adhere to Properties requirements.
  • Boolean - They have no " " surrounding them and only contain values "true" or "false". Can't adhere to Properties requirements.
  • Null - They have no " " surrounding them and only contain values "null". Can't adhere to Properties requirements.
  • JSON Object - Has a { bracket following the : character.
  • Array - Has a [ bracket following the : character.
Submitted by Dajeki - 5 years ago