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
/
(?:^([A-Y])?\s*(\d{0,4})\s?([a-d])?)|(?:^((?:\w+)|(?:\w+,)|(?:\w+, \w*))?\s*((?:&{1,2}|and|or|\|{2})\s+(?:.*)*)*)
/
gm
1st Alternative
(?:^([A-Y])?\s*(\d{0,4})\s?([a-d])?)
Non-capturing group
(?:^([A-Y])?\s*(\d{0,4})\s?([a-d])?)
^ asserts position at start of a line
1st Capturing Group
([A-Y])?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[A-Y]
A-Y matches a single character in the range between A (index 65) and Y (index 89) (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)
2nd Capturing Group
(\d{0,4})
\d
matches a digit (equivalent to [0-9])
{0,4} matches the previous token between 0 and 4 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 one times, as many times as possible, giving back as needed (greedy)
3rd Capturing Group
([a-d])?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[a-d]
a-d matches a single character in the range between a (index 97) and d (index 100) (case sensitive)
2nd Alternative
(?:^((?:\w+)|(?:\w+,)|(?:\w+, \w*))?\s*((?:&{1,2}|and|or|\|{2})\s+(?:.*)*)*)
Non-capturing group
(?:^((?:\w+)|(?:\w+,)|(?:\w+, \w*))?\s*((?:&{1,2}|and|or|\|{2})\s+(?:.*)*)*)
^ asserts position at start of a line
4th Capturing Group
((?:\w+)|(?:\w+,)|(?:\w+, \w*))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative
(?:\w+)
Non-capturing group
(?:\w+)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Alternative
(?:\w+,)
Non-capturing group
(?:\w+,)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one 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)
3rd Alternative
(?:\w+, \w*)
Non-capturing group
(?:\w+, \w*)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
,
matches the characters , literally (case sensitive)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
* 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)
5th Capturing Group
((?:&{1,2}|and|or|\|{2})\s+(?:.*)*)*
* 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
Non-capturing group
(?:&{1,2}|and|or|\|{2})
1st Alternative
&{1,2}
&
matches the character & with index 3810 (2616 or 468) literally (case sensitive)
2nd Alternative
and
and
matches the characters and literally (case sensitive)
3rd Alternative
or
or
matches the characters or literally (case sensitive)
4th Alternative
\|{2}
\|
matches the character | with index 12410 (7C16 or 1748) literally (case sensitive)
{2} matches the previous token exactly 2 times
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Non-capturing group
(?:.*)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
.
matches any character (except for line terminators)
* matches the previous token between zero and unlimited 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