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
"
(?P<assign_op>(\:|\!|\?)?=)|(?P<token_subst>$|%(\<\@))|(?P<token_ident>[\w\d\.\,]+)|(?P<target_name_match>%\.?)|(?P<token_rule_sep>[:])|(?P<incr_op>\n)|(?P<line_end>\n)|(?P<comment_op>#[.*]+$)|(?P<adjust_parser>[\$%\s\t\:])
"
gm
1st Alternative
(?P<assign_op>(\:|\!|\?)?=)
Named Capture Group assign_op
(?P<assign_op>(\:|\!|\?)?=)
2nd Capturing Group
(\:|\!|\?)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative
\:
\: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
2nd Alternative
\!
\! matches the character ! with index 3310 (2116 or 418) literally (case sensitive)
3rd Alternative
\?
\? matches the character ? with index 6310 (3F16 or 778) literally (case sensitive)
= matches the character = with index 6110 (3D16 or 758) literally (case sensitive)
2nd Alternative
(?P<token_subst>$|%(\<\@))
Named Capture Group token_subst
(?P<token_subst>$|%(\<\@))
1st Alternative
$
$ asserts position at the end of a line
2nd Alternative
%(\<\@)
% matches the character % with index 3710 (2516 or 458) literally (case sensitive)
4th Capturing Group
(\<\@)
\< matches the character < with index 6010 (3C16 or 748) literally (case sensitive)
\@ matches the character @ with index 6410 (4016 or 1008) literally (case sensitive)
3rd Alternative
(?P<token_ident>[\w\d\.\,]+)
Named Capture Group token_ident
(?P<token_ident>[\w\d\.\,]+)
Match a single character present in the list below
[\w\d\.\,]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
\d matches a digit (equivalent to [0-9])
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
4th Alternative
(?P<target_name_match>%\.?)
Named Capture Group target_name_match
(?P<target_name_match>%\.?)
% matches the character % with index 3710 (2516 or 458) literally (case sensitive)
\.
matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
5th Alternative
(?P<token_rule_sep>[:])
Named Capture Group token_rule_sep
(?P<token_rule_sep>[:])
Match a single character present in the list below
[:]
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
6th Alternative
(?P<incr_op>\n)
Named Capture Group incr_op
(?P<incr_op>\n)
\n matches a line-feed (newline) character (ASCII 10)
7th Alternative
(?P<line_end>\n)
Named Capture Group line_end
(?P<line_end>\n)
\n matches a line-feed (newline) character (ASCII 10)
8th Alternative
(?P<comment_op>#[.*]+$)
Named Capture Group comment_op
(?P<comment_op>#[.*]+$)
# matches the character # with index 3510 (2316 or 438) literally (case sensitive)
Match a single character present in the list below
[.*]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
.*
matches a single character in the list .* (case sensitive)
$ asserts position at the end of a line
9th Alternative
(?P<adjust_parser>[\$%\s\t\:])
Named Capture Group adjust_parser
(?P<adjust_parser>[\$%\s\t\:])
Match a single character present in the list below
[\$%\s\t\:]
\$ matches the character $ with index 3610 (2416 or 448) literally (case sensitive)
% matches the character % with index 3710 (2516 or 458) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v  ])
\t matches a tab character (ASCII 9)
\: matches the character : with index 5810 (3A16 or 728) 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.

Regular Expression
No Match

r"
"
gm

Test String

Code Generator

Generated Code

Loading code sample...
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report.
For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php