Regular Expressions 101

@regex101
Donate
Sponsor
Contact
Bug Reports & Feedback
Wiki
Whats new?

Save & Share

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

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python 2.7
  • 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)
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)
Comment: if blue isn't there, fail without delay
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)
Comment: start non-capture group
Comment: the aim is to skip lines that don't contain blue
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 )
Comment: With each line skipped, let Group 1 capture
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
Comment: end lookahead
Comment: end counter-line-skipper: zero or more times
Comment: the possessive + forbids backtracking
.
matches any character
Comment: lazily match any chars up to...
Comment: match blue
.
matches any character
Comment: Get to the end of the data
Comment: match a tilde
\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
Comment: drop what we matched so far
\d
matches a digit (equivalent to [0-9])
Comment: match digits. This is the match!
Positive Lookahead
(?=(?(1)\1))
Assert that the Regex below matches
Comment: 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...