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
"
([^\d\s]+)(\s+)(\d{1,2})(th|st|nd|rd)?([\/\s\-\.\,])(\s*)(\d{2,4})
"
g
1st Capturing Group
([^\d\s]+)
Match a single character not present in the list below
[^\d\s]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
\s matches any whitespace character (equivalent to [\r\n\t\f\v  ])
2nd Capturing Group
(\s+)
\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)
3rd Capturing Group
(\d{1,2})
\d
matches a digit (equivalent to [0-9])
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
4th Capturing Group
(th|st|nd|rd)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative
th
th
matches the characters th literally (case sensitive)
2nd Alternative
st
st
matches the characters st literally (case sensitive)
3rd Alternative
nd
nd
matches the characters nd literally (case sensitive)
4th Alternative
rd
rd
matches the characters rd literally (case sensitive)
5th Capturing Group
([\/\s\-\.\,])
Match a single character present in the list below
[\/\s\-\.\,]
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v  ])
\- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
6th Capturing Group
(\s*)
\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)
7th Capturing Group
(\d{2,4})
\d
matches a digit (equivalent to [0-9])
{2,4} matches the previous token between 2 and 4 times, as many times as possible, giving back as needed (greedy)
Global pattern flags
g modifier: global. All matches (don't return after first match)
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
g

Test String