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)(?P<space>[\s\n]*))(?(DEFINE)(?P<string>("[^"]*")))(?(DEFINE)(?P<integer>\d+))(?(DEFINE)(?P<PValue>((?P>string)|(?P>integer))))(?(DEFINE)(?P<PSV>(?P>space):(?P>space)))(?(DEFINE)(?P<PSP>(?P>space),(?P>space)))(?(DEFINE)(?P<PName>(?P>string))(?>(?P>PSV)))(?(DEFINE)(?P<property>(?P>PName)(?>(?P>PSV))(?P>PValue)(?>(?P>PSP)?)))"progressive"(?P>PSV)\[(?P>space)({(?P>property)*}(?P>PSP)?)*]}
/
gm
Subpattern definition construct
(?(DEFINE)(?P<space>[\s\n]*))
May only be used to define functions. No matching is done in this group.
Named Capture Group space
(?P<space>[\s\n]*)
Match a single character present in the list below
[\s\n]
* 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 ])
\n matches a line-feed (newline) character (ASCII 10)
Subpattern definition construct
(?(DEFINE)(?P<string>("[^"]*")))
May only be used to define functions. No matching is done in this group.
Named Capture Group string
(?P<string>("[^"]*"))
3rd Capturing Group
("[^"]*")
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
Match a single character not present in the list below
[^"]
* matches the previous token between zero 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 3410 (2216 or 428) literally (case sensitive)
Subpattern definition construct
(?(DEFINE)(?P<integer>\d+))
May only be used to define functions. No matching is done in this group.
Named Capture Group integer
(?P<integer>\d+)
\d
matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Subpattern definition construct
(?(DEFINE)(?P<PValue>((?P>string)|(?P>integer))))
May only be used to define functions. No matching is done in this group.
Named Capture Group PValue
(?P<PValue>((?P>string)|(?P>integer)))
6th Capturing Group
((?P>string)|(?P>integer))
1st Alternative
(?P>string)
(?P>string) recurses the subpattern named string
2nd Alternative
(?P>integer)
(?P>integer) recurses the subpattern named integer
Subpattern definition construct
(?(DEFINE)(?P<PSV>(?P>space):(?P>space)))
May only be used to define functions. No matching is done in this group.
Named Capture Group PSV
(?P<PSV>(?P>space):(?P>space))
(?P>space) recurses the subpattern named space
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
(?P>space) recurses the subpattern named space
Subpattern definition construct
(?(DEFINE)(?P<PSP>(?P>space),(?P>space)))
May only be used to define functions. No matching is done in this group.
Named Capture Group PSP
(?P<PSP>(?P>space),(?P>space))
(?P>space) recurses the subpattern named space
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
(?P>space) recurses the subpattern named space
Subpattern definition construct
(?(DEFINE)(?P<PName>(?P>string))(?>(?P>PSV)))
May only be used to define functions. No matching is done in this group.
Named Capture Group PName
(?P<PName>(?P>string))
(?P>string) recurses the subpattern named string
Atomic Group
(?>(?P>PSV))
Non-capturing group that discards backtracking positions once matched.
(?P>PSV) recurses the subpattern named PSV
Subpattern definition construct
(?(DEFINE)(?P<property>(?P>PName)(?>(?P>PSV))(?P>PValue)(?>(?P>PSP)?)))
May only be used to define functions. No matching is done in this group.
Named Capture Group property
(?P<property>(?P>PName)(?>(?P>PSV))(?P>PValue)(?>(?P>PSP)?))
(?P>PName) recurses the subpattern named PName
Atomic Group
(?>(?P>PSV))
Non-capturing group that discards backtracking positions once matched.
(?P>PSV) recurses the subpattern named PSV
(?P>PValue) recurses the subpattern named PValue
Atomic Group
(?>(?P>PSP)?)
Non-capturing group that discards backtracking positions once matched.
(?P>PSP)
recurses the subpattern named PSP
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
"progressive"
matches the characters "progressive" literally (case sensitive)
(?P>PSV) recurses the subpattern named PSV
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
(?P>space) recurses the subpattern named space
11th Capturing Group
({(?P>property)*}(?P>PSP)?)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
{ matches the character { with index 12310 (7B16 or 1738) literally (case sensitive)
(?P>property)
recurses the subpattern named property
* 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)
(?P>PSP)
recurses the subpattern named PSP
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
]}
matches the characters ]} literally (case sensitive)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
gm

Test String