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
/
\b((?:\d{1,3})(?:([,. ']?)\d{3})?(?:\2\d{3})*)(?:(?:|(?!\2))([,./])(\d+))?\b
/
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
1st Capturing Group
((?:\d{1,3})(?:([,. ']?)\d{3})?(?:\2\d{3})*)
Non-capturing group
(?:\d{1,3})
\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)
Non-capturing group
(?:([,. ']?)\d{3})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group
([,. ']?)
Match a single character present in the list below
[,. ']
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
,. '
matches a single character in the list ,. ' (case sensitive)
\d
matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
Non-capturing group
(?:\2\d{3})*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\2 matches the same text as most recently matched by the 2nd capturing group
\d
matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
Non-capturing group
(?:(?:|(?!\2))([,./])(\d+))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Non-capturing group
(?:|(?!\2))
1st Alternative null, matches any position
2nd Alternative
(?!\2)
Negative Lookahead
(?!\2)
Assert that the Regex below does not match
\2 matches the same text as most recently matched by the 2nd capturing group
3rd Capturing Group
([,./])
Match a single character present in the list below
[,./]
,./
matches a single character in the list ,./ (case sensitive)
4th 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)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
Your regular expression does not match the subject string.

Regular Expression
No Match

/
/

Test String