Regular Expressions 101

Save & Share

  • Save Regex
    ctrl+s
  • Update Regex
    ctrl+⇧+s

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8

Function

  • Match
  • Substitution
  • List
  • Unit Tests
/
(?xsm) # free-spacing mode, DOTALL, multi-line (?=.*?blue) # if blue isn't there, fail without delay ########### LINE SKIPPER / COUNTER ############ (?: # start non-capture group # the aim is to skip lines that don't contain blue # and capture a corresponding digit sequence (?: # skip one line that doesn't contain blue ^ # beginning of line (?:(?!blue)[^\r\n])* # zero or more chars # that do not start blue (?:\r?\n) # newline chars ) # With each line skipped, let Group 1 capture # an ever-growing portion of the string of numbers (?= # lookahead .* # Go to the end of the file ( # start Group 1 ~\d+ # match a tilde and digits (?(1)\1) # if Group 1 is set, match Group 1 ) # end Group 1 ) # end lookahead )*+ # end counter-line-skipper: zero or more times # the possessive + forbids backtracking .*? # lazily match any chars up to... blue # match blue .* # Get to the end of the data ~ # match a tilde \K # drop what we matched so far \d+ # match digits. This is the match! (?=(?(1)\1)) # if Group 1 has been set, match it
/
(?xsm)
match the remainder of the pattern with the following effective flags: xsm
x modifier: extended. Spaces and text after a # in the pattern are ignored
s modifier: single line. Dot matches newline characters
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
# free-spacing mode, DOTALL, multi-line
free-spacing mode, DOTALL, multi-line
Comment: free-spacing mode, DOTALL, multi-line
Positive Lookahead
(?=.*?blue)
Assert that the Regex below matches
.
matches any character
*? matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
blue
matches the characters blue literally (case sensitive)
# if blue isn't there, fail without delay
if blue isn't there, fail without delay
Comment: if blue isn't there, fail without delay
########### LINE SKIPPER / COUNTER ############
########## LINE SKIPPER / COUNTER ############
Comment: ########## LINE SKIPPER / COUNTER ############
Non-capturing group
(?: # start non-capture group # the aim is to skip lines that don't contain blue # and capture a corresponding digit sequence (?: # skip one line that doesn't contain blue ^ # beginning of line (?:(?!blue)[^\r\n])* # zero or more chars # that do not start blue (?:\r?\n) # newline chars ) # With each line skipped, let Group 1 capture # an ever-growing portion of the string of numbers (?= # lookahead .* # Go to the end of the file ( # start Group 1 ~\d+ # match a tilde and digits (?(1)\1) # if Group 1 is set, match Group 1 ) # end Group 1 ) # end lookahead )*+
*+ matches the previous token between zero and unlimited times, as many times as possible, without giving back (possessive)
# start non-capture group
start non-capture group
Comment: start non-capture group
# the aim is to skip lines that don't contain blue
the aim is to skip lines that don't contain blue
Comment: the aim is to skip lines that don't contain blue
# and capture a corresponding digit sequence
and capture a corresponding digit sequence
Comment: and capture a corresponding digit sequence
Non-capturing group
(?: # skip one line that doesn't contain blue ^ # beginning of line (?:(?!blue)[^\r\n])* # zero or more chars # that do not start blue (?:\r?\n) # newline chars )
# skip one line that doesn't contain blue
skip one line that doesn't contain blue
Comment: skip one line that doesn't contain blue
^ asserts position at start of a line
# beginning of line
beginning of line
Comment: beginning of line
Non-capturing group
(?:(?!blue)[^\r\n])*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Negative Lookahead
(?!blue)
Assert that the Regex below does not match
blue
matches the characters blue literally (case sensitive)
Match a single character not present in the list below
[^\r\n]
# zero or more chars
zero or more chars
# that do not start blue
that do not start blue
Non-capturing group
(?:\r?\n)
# newline chars
newline chars
# With each line skipped, let Group 1 capture
With each line skipped, let Group 1 capture
Comment: With each line skipped, let Group 1 capture
# an ever-growing portion of the string of numbers
an ever-growing portion of the string of numbers
Comment: an ever-growing portion of the string of numbers
Positive Lookahead
(?= # lookahead .* # Go to the end of the file ( # start Group 1 ~\d+ # match a tilde and digits (?(1)\1) # if Group 1 is set, match Group 1 ) # end Group 1 )
Assert that the Regex below matches
# end lookahead
end lookahead
# end counter-line-skipper: zero or more times
end counter-line-skipper: zero or more times
Comment: end counter-line-skipper: zero or more times
# the possessive + forbids backtracking
the possessive + forbids backtracking
Comment: the possessive + forbids backtracking
.
matches any character
*? matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
# lazily match any chars up to... blue
lazily match any chars up to...
Comment: lazily match any chars up to...
b matches the character b with index 9810 (6216 or 1428) literally (case sensitive)
l matches the character l with index 10810 (6C16 or 1548) literally (case sensitive)
u matches the character u with index 11710 (7516 or 1658) literally (case sensitive)
e matches the character e with index 10110 (6516 or 1458) literally (case sensitive)
# match blue
match blue
Comment: match blue
.
matches any character
# Get to the end of the data ~
Get to the end of the data
# match a tilde
match a tilde
\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
# drop what we matched so far
drop what we matched so far
\d
matches a digit (equivalent to [0-9])
# match digits. This is the match!
match digits. This is the match!
Positive Lookahead
(?=(?(1)\1))
Assert that the Regex below matches
# if Group 1 has been set, match it
if Group 1 has been set, match it
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/

Test String

Substitution

Processing...

Regex Debugger

Please wait while your expression is being debugged...