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-increasing portion of the string of numbers (?= # lookahead [^~]+ # skip all chars that are not tildes ( # start Group 1 (?(1)\1) # if Group 1 is set, match Group 1 # (?>\1?) # alternate phrasing for the above ~\d+ # match a tilde and digits ) # 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 [^~]+ # match any non-tilde chars (?(1)\1) # if Group 1 has been set, match it # \1? # alternate phrasing for the above ~ # match a tilde \K # drop what we matched so far \d+ # match digits. This is the match!
/
(?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-increasing portion of the string of numbers (?= # lookahead [^~]+ # skip all chars that are not tildes ( # start Group 1 (?(1)\1) # if Group 1 is set, match Group 1 # (?>\1?) # alternate phrasing for the above ~\d+ # match a tilde and digits ) # 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-increasing portion of the string of numbers
Positive Lookahead
(?= # lookahead [^~]+ # skip all chars that are not tildes ( # start Group 1 (?(1)\1) # if Group 1 is set, match Group 1 # (?>\1?) # alternate phrasing for the above ~\d+ # match a tilde and digits ) # 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
Match a single character not present in the list below
[^~]
Comment: match any non-tilde chars
Conditional
(?(1)\1)
Comment: if Group 1 has been set, match it
Comment: \1? # alternate phrasing for the above
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!
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/

Test String