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
"
(?:\s|^) # Start of string or word boundary ( # Capture Group (?: # Non-Capture Group for X. (?: # Nested NCG for 0..255 (?:\d){1,2}| # Allow any single digit or any pair of digits or (?:1\d{2})| # 3 Digits starting with 1 or (?:2[0-4]\d)| # 3 Digists starting with 20..24 or (?:25[0-5]) # 250..255 ) # this should allow X=0..255 in the NCG \. # Must be followed by a literal . ){3} # X. Must repeat exactly 3 times (?: # NCG for final 0..255 (?:\d{1,2})| # Allow any single digit or any pair of digits or (?:1\d{2})| # 3 Digits starting with 1 or (?:2[0-4]\d)| # 3 digits starting 20..24 or (?:25[0-5]) # 250.255 ) # End of final 0.255 ) # End of capture group (?:\s|$) # Must be followed by EOS or wspace
"
gx
Non-capturing group
(?:\s|^)
1st Alternative
\s
\s matches any whitespace character (equivalent to [\r\n\t\f\v  ])
2nd Alternative
^
^ asserts position at start of the string
# Start of string or word boundary
Start of string or word boundary
Comment: Start of string or word boundary
1st Capturing Group
( # Capture Group (?: # Non-Capture Group for X. (?: # Nested NCG for 0..255 (?:\d){1,2}| # Allow any single digit or any pair of digits or (?:1\d{2})| # 3 Digits starting with 1 or (?:2[0-4]\d)| # 3 Digists starting with 20..24 or (?:25[0-5]) # 250..255 ) # this should allow X=0..255 in the NCG \. # Must be followed by a literal . ){3} # X. Must repeat exactly 3 times (?: # NCG for final 0..255 (?:\d{1,2})| # Allow any single digit or any pair of digits or (?:1\d{2})| # 3 Digits starting with 1 or (?:2[0-4]\d)| # 3 digits starting 20..24 or (?:25[0-5]) # 250.255 ) # End of final 0.255 )
# Capture Group
Capture Group
Comment: Capture Group
Non-capturing group
(?: # Non-Capture Group for X. (?: # Nested NCG for 0..255 (?:\d){1,2}| # Allow any single digit or any pair of digits or (?:1\d{2})| # 3 Digits starting with 1 or (?:2[0-4]\d)| # 3 Digists starting with 20..24 or (?:25[0-5]) # 250..255 ) # this should allow X=0..255 in the NCG \. # Must be followed by a literal . ){3}
{3} matches the previous token exactly 3 times
# Non-Capture Group for X.
Non-Capture Group for X.
Comment: Non-Capture Group for X.
Non-capturing group
(?: # Nested NCG for 0..255 (?:\d){1,2}| # Allow any single digit or any pair of digits or (?:1\d{2})| # 3 Digits starting with 1 or (?:2[0-4]\d)| # 3 Digists starting with 20..24 or (?:25[0-5]) # 250..255 )
1st Alternative
# Nested NCG for 0..255 (?:\d){1,2}
# Nested NCG for 0..255
Nested NCG for 0..255
Comment: Nested NCG for 0..255
Non-capturing group
(?:\d){1,2}
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
2nd Alternative
# Allow any single digit or any pair of digits or (?:1\d{2})
# Allow any single digit or any pair of digits or
Allow any single digit or any pair of digits or
Comment: Allow any single digit or any pair of digits or
Non-capturing group
(?:1\d{2})
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{2} matches the previous token exactly 2 times
3rd Alternative
# 3 Digits starting with 1 or (?:2[0-4]\d)
# 3 Digits starting with 1 or
3 Digits starting with 1 or
Comment: 3 Digits starting with 1 or
Non-capturing group
(?:2[0-4]\d)
2 matches the character 2 with index 5010 (3216 or 628) literally (case sensitive)
Match a single character present in the list below
[0-4]
0-4 matches a single character in the range between 0 (index 48) and 4 (index 52) (case sensitive)
\d matches a digit (equivalent to [0-9])
4th Alternative
# 3 Digists starting with 20..24 or (?:25[0-5]) # 250..255
# 3 Digists starting with 20..24 or
3 Digists starting with 20..24 or
Comment: 3 Digists starting with 20..24 or
Non-capturing group
(?:25[0-5])
25
matches the characters 25 literally (case sensitive)
Match a single character present in the list below
[0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (case sensitive)
# 250..255
250..255
Comment: 250..255
# this should allow X=0..255 in the NCG
this should allow X=0..255 in the NCG
Comment: this should allow X=0..255 in the NCG
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
# Must be followed by a literal .
Must be followed by a literal .
Comment: Must be followed by a literal .
# X. Must repeat exactly 3 times
X. Must repeat exactly 3 times
Comment: X. Must repeat exactly 3 times
Non-capturing group
(?: # NCG for final 0..255 (?:\d{1,2})| # Allow any single digit or any pair of digits or (?:1\d{2})| # 3 Digits starting with 1 or (?:2[0-4]\d)| # 3 digits starting 20..24 or (?:25[0-5]) # 250.255 )
1st Alternative
# NCG for final 0..255 (?:\d{1,2})
# NCG for final 0..255
NCG for final 0..255
Comment: NCG for final 0..255
Non-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)
2nd Alternative
# Allow any single digit or any pair of digits or (?:1\d{2})
# Allow any single digit or any pair of digits or
Allow any single digit or any pair of digits or
Comment: Allow any single digit or any pair of digits or
Non-capturing group
(?:1\d{2})
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{2} matches the previous token exactly 2 times
3rd Alternative
# 3 Digits starting with 1 or (?:2[0-4]\d)
# 3 Digits starting with 1 or
3 Digits starting with 1 or
Comment: 3 Digits starting with 1 or
Non-capturing group
(?:2[0-4]\d)
2 matches the character 2 with index 5010 (3216 or 628) literally (case sensitive)
Match a single character present in the list below
[0-4]
0-4 matches a single character in the range between 0 (index 48) and 4 (index 52) (case sensitive)
\d matches a digit (equivalent to [0-9])
4th Alternative
# 3 digits starting 20..24 or (?:25[0-5]) # 250.255
# 3 digits starting 20..24 or
3 digits starting 20..24 or
Comment: 3 digits starting 20..24 or
Non-capturing group
(?:25[0-5])
25
matches the characters 25 literally (case sensitive)
Match a single character present in the list below
[0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (case sensitive)
# 250.255
250.255
Comment: 250.255
# End of final 0.255
End of final 0.255
Comment: End of final 0.255
# End of capture group
End of capture group
Comment: End of capture group
Non-capturing group
(?:\s|$)
1st Alternative
\s
\s matches any whitespace character (equivalent to [\r\n\t\f\v  ])
2nd Alternative
$
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
Comment: Must be followed by EOS or wspace
Global pattern flags
g modifier: global. All matches (don't return after first match)
x modifier: extended. Spaces and text after a # in the pattern are ignored
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
gx

Test String