Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8

Function

  • Match
  • Substitution
  • List
  • Unit Tests
/
(?(DEFINE) # Note that everything is atomic, JSON does not need backtracking if it's valid # and this prevents catastrophic backtracking (?<json>(?>\s*(?&object)\s*|\s*(?&array)\s*)) (?<object>(?>\{\s*(?>(?&pair)(?>\s*,\s*(?&pair))*)?\s*\})) (?<pair>(?>(?&STRING)\s*:\s*(?&value))) (?<array>(?>\[\s*(?>(?&value)(?>\s*,\s*(?&value))*)?\s*\])) (?<value>(?>true|false|null|(?&STRING)|(?&NUMBER)|(?&object)|(?&array))) (?<STRING>(?>"(?>\\(?>["\\\/bfnrt]|u[a-fA-F0-9]{4})|[^"\\\0-\x1F\x7F]+)*")) (?<NUMBER>(?>-?(?>0|[1-9][0-9]*)(?>\.[0-9]+)?(?>[eE][+-]?[0-9]+)?)) ) \A(?&json)\z
/
x
Subpattern definition construct
(?(DEFINE) # Note that everything is atomic, JSON does not need backtracking if it's valid # and this prevents catastrophic backtracking (?<json>(?>\s*(?&object)\s*|\s*(?&array)\s*)) (?<object>(?>\{\s*(?>(?&pair)(?>\s*,\s*(?&pair))*)?\s*\})) (?<pair>(?>(?&STRING)\s*:\s*(?&value))) (?<array>(?>\[\s*(?>(?&value)(?>\s*,\s*(?&value))*)?\s*\])) (?<value>(?>true|false|null|(?&STRING)|(?&NUMBER)|(?&object)|(?&array))) (?<STRING>(?>"(?>\\(?>["\\\/bfnrt]|u[a-fA-F0-9]{4})|[^"\\\0-\x1F\x7F]+)*")) (?<NUMBER>(?>-?(?>0|[1-9][0-9]*)(?>\.[0-9]+)?(?>[eE][+-]?[0-9]+)?)) )
May only be used to define functions. No matching is done in this group.
# Note that everything is atomic, JSON does not need backtracking if it's valid
Note that everything is atomic, JSON does not need backtracking if it's valid
Comment: Note that everything is atomic, JSON does not need backtracking if it's valid
# and this prevents catastrophic backtracking
and this prevents catastrophic backtracking
Comment: and this prevents catastrophic backtracking
Named Capture Group json
(?<json>(?>\s*(?&object)\s*|\s*(?&array)\s*))
Atomic Group
(?>\s*(?&object)\s*|\s*(?&array)\s*)
Non-capturing group that discards backtracking positions once matched.
1st Alternative
\s*(?&object)\s*
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?&object) recurses the subpattern named object
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Alternative
\s*(?&array)\s*
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?&array) recurses the subpattern named array
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Named Capture Group object
(?<object>(?>\{\s*(?>(?&pair)(?>\s*,\s*(?&pair))*)?\s*\}))
Atomic Group
(?>\{\s*(?>(?&pair)(?>\s*,\s*(?&pair))*)?\s*\})
Non-capturing group that discards backtracking positions once matched.
\{ matches the character { with index 12310 (7B16 or 1738) literally (case sensitive)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Atomic Group
(?>(?&pair)(?>\s*,\s*(?&pair))*)?
Non-capturing group that discards backtracking positions once matched.
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
(?&pair) recurses the subpattern named pair
Atomic Group
(?>\s*,\s*(?&pair))*
Non-capturing group that discards backtracking positions once matched.
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?&pair) recurses the subpattern named pair
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\} matches the character } with index 12510 (7D16 or 1758) literally (case sensitive)
Named Capture Group pair
(?<pair>(?>(?&STRING)\s*:\s*(?&value)))
Atomic Group
(?>(?&STRING)\s*:\s*(?&value))
Non-capturing group that discards backtracking positions once matched.
(?&STRING) recurses the subpattern named STRING
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?&value) recurses the subpattern named value
Named Capture Group array
(?<array>(?>\[\s*(?>(?&value)(?>\s*,\s*(?&value))*)?\s*\]))
Atomic Group
(?>\[\s*(?>(?&value)(?>\s*,\s*(?&value))*)?\s*\])
Non-capturing group that discards backtracking positions once matched.
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Atomic Group
(?>(?&value)(?>\s*,\s*(?&value))*)?
Non-capturing group that discards backtracking positions once matched.
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
(?&value) recurses the subpattern named value
Atomic Group
(?>\s*,\s*(?&value))*
Non-capturing group that discards backtracking positions once matched.
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?&value) recurses the subpattern named value
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
Named Capture Group value
(?<value>(?>true|false|null|(?&STRING)|(?&NUMBER)|(?&object)|(?&array)))
Atomic Group
(?>true|false|null|(?&STRING)|(?&NUMBER)|(?&object)|(?&array))
Non-capturing group that discards backtracking positions once matched.
1st Alternative
true
true
matches the characters true literally (case sensitive)
2nd Alternative
false
false
matches the characters false literally (case sensitive)
3rd Alternative
null
null
matches the characters null literally (case sensitive)
4th Alternative
(?&STRING)
(?&STRING) recurses the subpattern named STRING
5th Alternative
(?&NUMBER)
(?&NUMBER) recurses the subpattern named NUMBER
6th Alternative
(?&object)
7th Alternative
(?&array)
Named Capture Group STRING
(?<STRING>(?>"(?>\\(?>["\\\/bfnrt]|u[a-fA-F0-9]{4})|[^"\\\0-\x1F\x7F]+)*"))
Atomic Group
(?>"(?>\\(?>["\\\/bfnrt]|u[a-fA-F0-9]{4})|[^"\\\0-\x1F\x7F]+)*")
Non-capturing group that discards backtracking positions once matched.
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
Atomic Group
(?>\\(?>["\\\/bfnrt]|u[a-fA-F0-9]{4})|[^"\\\0-\x1F\x7F]+)*
Non-capturing group that discards backtracking positions once matched.
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
1st Alternative
\\(?>["\\\/bfnrt]|u[a-fA-F0-9]{4})
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
Atomic Group
(?>["\\\/bfnrt]|u[a-fA-F0-9]{4})
Non-capturing group that discards backtracking positions once matched.
1st Alternative
["\\\/bfnrt]
Match a single character present in the list below
["\\\/bfnrt]
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
bfnrt
matches a single character in the list bfnrt (case sensitive)
2nd Alternative
u[a-fA-F0-9]{4}
u matches the character u with index 11710 (7516 or 1658) literally (case sensitive)
Match a single character present in the list below
[a-fA-F0-9]
{4} matches the previous token exactly 4 times
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Alternative
[^"\\\0-\x1F\x7F]+
Match a single character not present in the list below
[^"\\\0-\x1F\x7F]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
\0-\x1F matches a single character in the range between (index 0) and  (index 31) (case sensitive)
\x7F matches the character  with index 7F16 (12710 or 1778) literally (case sensitive)
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
Named Capture Group NUMBER
(?<NUMBER>(?>-?(?>0|[1-9][0-9]*)(?>\.[0-9]+)?(?>[eE][+-]?[0-9]+)?))
Atomic Group
(?>-?(?>0|[1-9][0-9]*)(?>\.[0-9]+)?(?>[eE][+-]?[0-9]+)?)
Non-capturing group that discards backtracking positions once matched.
-
matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
Atomic Group
(?>0|[1-9][0-9]*)
Non-capturing group that discards backtracking positions once matched.
1st Alternative
0
0 matches the character 0 with index 4810 (3016 or 608) literally (case sensitive)
2nd Alternative
[1-9][0-9]*
Match a single character present in the list below
[1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case sensitive)
Match a single character present in the list below
[0-9]
Atomic Group
(?>\.[0-9]+)?
Non-capturing group that discards backtracking positions once matched.
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
Match a single character present in the list below
[0-9]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
Atomic Group
(?>[eE][+-]?[0-9]+)?
Non-capturing group that discards backtracking positions once matched.
\A asserts position at start of the string
(?&json) recurses the subpattern named json
\z asserts position at the end of the string
Global pattern flags
x modifier: extended. Spaces and text after a # in the pattern are ignored
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
x

Test String