Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

~
(?(DEFINE) # Start of definitions (?P<str_double_quotes> (?<!\\) # Not escaped " # Match a double quote (?: # Non-capturing group [^\\] # Match anything not a backslash | # Or \\. # Match a backslash and a single character (ie: an escaped character) )*? # Repeat the non-capturing group zero or more times, ungreedy/lazy " # Match the ending double quote ) (?P<str_single_quotes> (?<!\\) # Not escaped ' # Match a single quote (?: # Non-capturing group [^\\] # Match anything not a backslash | # Or \\. # Match a backslash and a single character (ie: an escaped character) )*? # Repeat the non-capturing group zero or more times, ungreedy/lazy ' # Match the ending single quote ) (?P<brackets> \( # Match an opening bracket (?: # A non capturing group (?&str_double_quotes) # Recurse/use the str_double_quotes pattern | # Or (?&str_single_quotes) # Recurse/use the str_single_quotes pattern | # Or [^()] # Anything not a bracket | # Or (?&brackets) # Recurse the bracket pattern )* \) ) ) # End of definitions # Let's start matching for real now: _n? # Match _ or _n \s* # Optional white spaces (?P<results>(?&brackets)) # Recurse/use the brackets pattern and put it in the results group
~
sxg