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{4}(-\d{2}){2}T(\d{2}\W?){5}\s(\d{1,3}(\.|\b)){4}\s\d\s\d{10}\.\d{9}\s\w+\s
/
gm
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
1st Capturing Group
(-\d{2}){2}
{2} matches the previous token exactly 2 times
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
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{2} matches the previous token exactly 2 times
T matches the character T with index 8410 (5416 or 1248) literally (case sensitive)
2nd Capturing Group
(\d{2}\W?){5}
{5} matches the previous token exactly 5 times
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
\d
matches a digit (equivalent to [0-9])
{2} matches the previous token exactly 2 times
\W
matches any non-word character (equivalent to [^a-zA-Z0-9_])
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
3rd Capturing Group
(\d{1,3}(\.|\b)){4}
{4} matches the previous token exactly 4 times
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
\d
matches a digit (equivalent to [0-9])
{1,3} matches the previous token between 1 and 3 times, as many times as possible, giving back as needed (greedy)
4th Capturing Group
(\.|\b)
1st Alternative
\.
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
2nd Alternative
\b
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\d matches a digit (equivalent to [0-9])
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\d
matches a digit (equivalent to [0-9])
{10} matches the previous token exactly 10 times
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{9} matches the previous token exactly 9 times
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\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)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
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