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 ###### Recursive Section ###### # This section aims to balance empty lines with digits, i.e. # emptyLine,emptyLine,emptyLine ... ~1~2~3 # The last digit block is captured to Group 2, e.g. ~3 (?= # lookahead ( # Group 1 (?: # skip one line that doesn't contain blue ^ # start of line (?:(?!blue)[^\r\n])* # zero or more chars not followed by blue (?:\r?\n) # newline ) (?:(?1)|[^~]+) # recurse Group 1 OR match all non-tilde chars (~\d+) # match a sequence of digits )? # End Group 1 ) # End lookahead. # Group 2, if set, now contains the number of lines skipped .*? # lazily match chars up to... blue # match blue .*? # lazily match chars up to... (?(2)\2) # if Group 2 is set, match Group 2 ~ # Match the next tilde \K # drop what was matched so far \d+ # match the next 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)
# 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
###### Recursive Section ######
##### Recursive Section ######
Comment: ##### Recursive Section ######
# This section aims to balance empty lines with digits, i.e.
This section aims to balance empty lines with digits, i.e.
Comment: This section aims to balance empty lines with digits, i.e.
# emptyLine,emptyLine,emptyLine ... ~1~2~3
emptyLine,emptyLine,emptyLine ... ~1~2~3
Comment: emptyLine,emptyLine,emptyLine ... ~1~2~3
# The last digit block is captured to Group 2, e.g. ~3
The last digit block is captured to Group 2, e.g. ~3
Comment: The last digit block is captured to Group 2, e.g. ~3
Positive Lookahead
(?= # lookahead ( # Group 1 (?: # skip one line that doesn't contain blue ^ # start of line (?:(?!blue)[^\r\n])* # zero or more chars not followed by blue (?:\r?\n) # newline ) (?:(?1)|[^~]+) # recurse Group 1 OR match all non-tilde chars (~\d+) # match a sequence of digits )? # End Group 1 )
Assert that the Regex below matches
# lookahead
lookahead
Comment: lookahead
1st Capturing Group
( # Group 1 (?: # skip one line that doesn't contain blue ^ # start of line (?:(?!blue)[^\r\n])* # zero or more chars not followed by blue (?:\r?\n) # newline ) (?:(?1)|[^~]+) # recurse Group 1 OR match all non-tilde chars (~\d+) # match a sequence of digits )?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
# Group 1
Group 1
Comment: Group 1
Non-capturing group
(?: # skip one line that doesn't contain blue ^ # start of line (?:(?!blue)[^\r\n])* # zero or more chars not followed by blue (?:\r?\n) # newline )
# 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
# start of line
start of line
Non-capturing group
(?:(?!blue)[^\r\n])*
# zero or more chars not followed by blue
zero or more chars not followed by blue
Non-capturing group
(?:\r?\n)
# newline
newline
Non-capturing group
(?:(?1)|[^~]+)
# recurse Group 1 OR match all non-tilde chars
recurse Group 1 OR match all non-tilde chars
2nd Capturing Group
(~\d+)
# match a sequence of digits
match a sequence of digits
# End Group 1
End Group 1
Comment: End Group 1
# End lookahead.
End lookahead.
Comment: End lookahead.
# Group 2, if set, now contains the number of lines skipped
Group 2, if set, now contains the number of lines skipped
Comment: Group 2, if set, now contains the number of lines skipped
.
matches any character
# lazily match chars up to... blue
lazily match chars up to...
# match blue
match blue
.
matches any character
# lazily match chars up to...
lazily match chars up to...
Conditional
(?(2)\2)
# if Group 2 is set, match Group 2 ~
if Group 2 is set, match Group 2
# Match the next tilde
Match the next tilde
\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
# drop what was matched so far
drop what was matched so far
\d
matches a digit (equivalent to [0-9])
# match the next digits: this is the match
match the next 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