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
/
(\+?(\b1)?[\ .\/-]?((?(2)|(\b))|(\())\d{3}(?(?<=\(\d{3})\)|)[\ .\/-]?)?(?(1)|\b)\d{3}[\ .\/-]?\d{4}[\ ]?([xX][\ ]?\d{1,5})?\b
/
gm
1st Capturing Group
(\+?(\b1)?[\ .\/-]?((?(2)|(\b))|(\())\d{3}(?(?<=\(\d{3})\)|)[\ .\/-]?)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\+
matches the character + with index 4310 (2B16 or 538) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group
(\b1)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
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 the character with index 3210 (2016 or 408) literally (case sensitive)
. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
3rd Capturing Group
((?(2)|(\b))|(\())
1st Alternative
(?(2)|(\b))
Conditional
(?(2)|(\b))
Conditionally matches one of two options depending on whether the 2nd capturing group matched
If condition is met, match the following regex null, matches any position
Else match the following regex
(\b)
4th Capturing Group
(\b)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
2nd Alternative
(\()
5th Capturing Group
(\()
\( matches the character ( with index 4010 (2816 or 508) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
Conditional
(?(?<=\(\d{3})\)|)
Conditionally matches one of two options depending on whether the lookaround matches
Positive Lookbehind
(?<=\(\d{3})
Assert that the Regex below matches
\( matches the character ( with index 4010 (2816 or 508) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
If condition is met, match the following regex
\)
\) matches the character ) with index 4110 (2916 or 518) literally (case sensitive)
Else match the following regex null, matches any position
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 the character with index 3210 (2016 or 408) literally (case sensitive)
. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
Conditional
(?(1)|\b)
Conditionally matches one of two options depending on whether the 1st capturing group matched
If condition is met, match the following regex null, matches any position
Else match the following regex
\b
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
\d
matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
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 the character with index 3210 (2016 or 408) literally (case sensitive)
. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
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 the character with index 3210 (2016 or 408) literally (case sensitive)
6th Capturing Group
([xX][\ ]?\d{1,5})?
? 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
[xX]
xX
matches a single character in the list xX (case sensitive)
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 the character with index 3210 (2016 or 408) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{1,5} matches the previous token between 1 and 5 times, as many times as possible, giving back as needed (greedy)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
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