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
"
^SW(TS|L|M)-(\d+)([:,] )([wW][iI][pP])?(?(4)[ \w]{3,}|(?!([wW][iI][pP]))[ \w]{3,}).*$
"
gm
^ asserts position at start of a line
SW
matches the characters SW literally (case sensitive)
1st Capturing Group
(TS|L|M)
1st Alternative
TS
TS
matches the characters TS literally (case sensitive)
2nd Alternative
L
L matches the character L with index 7610 (4C16 or 1148) literally (case sensitive)
3rd Alternative
M
M matches the character M with index 7710 (4D16 or 1158) literally (case sensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
2nd Capturing Group
(\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)
3rd Capturing Group
([:,] )
Match a single character present in the list below
[:,]
:,
matches a single character in the list :, (case sensitive)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
4th Capturing Group
([wW][iI][pP])?
? 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
[wW]
wW
matches a single character in the list wW (case sensitive)
Match a single character present in the list below
[iI]
iI
matches a single character in the list iI (case sensitive)
Match a single character present in the list below
[pP]
pP
matches a single character in the list pP (case sensitive)
Conditional
(?(4)[ \w]{3,}|(?!([wW][iI][pP]))[ \w]{3,})
Conditionally matches one of two options depending on whether the 4th capturing group matched
If condition is met, match the following regex
[ \w]{3,}
Match a single character present in the list below
[ \w]
{3,} matches the previous token between 3 and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
\w matches any word character (equivalent to [a-zA-Z0-9_])
Else match the following regex
(?!([wW][iI][pP]))[ \w]{3,}
Negative Lookahead
(?!([wW][iI][pP]))
Assert that the Regex below does not match
5th Capturing Group
([wW][iI][pP])
Match a single character present in the list below
[wW]
wW
matches a single character in the list wW (case sensitive)
Match a single character present in the list below
[iI]
iI
matches a single character in the list iI (case sensitive)
Match a single character present in the list below
[pP]
pP
matches a single character in the list pP (case sensitive)
Match a single character present in the list below
[ \w]
{3,} matches the previous token between 3 and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
\w matches any word character (equivalent to [a-zA-Z0-9_])
.
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)
$ asserts position at the end of a line
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