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 (17)
/
^(.(?=.* (\2.|)))+x.* \2.?.?x
/
gm
^ asserts position at start of a line
1st Capturing Group
(.(?=.* (\2.|)))+
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
. matches any character (except for line terminators)
Positive Lookahead
(?=.* (\2.|))
Assert that the Regex below matches
.
matches any character (except for line terminators)
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 1010 (A16 or 128) literally (case sensitive)
2nd Capturing Group
(\2.|)
1st Alternative
\2.
\2 matches the same text as most recently matched by the 2nd capturing group
. matches any character (except for line terminators)
2nd Alternative null, matches any position
x matches the character x with index 12010 (7816 or 1708) literally (case sensitive)
.
matches any character (except for line terminators)
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 1010 (A16 or 128) literally (case sensitive)
\2 matches the same text as most recently matched by the 2nd capturing group
.
matches any character (except for line terminators)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
.
matches any character (except for line terminators)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
x matches the character x with index 12010 (7816 or 1708) literally (case sensitive)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Regular Expression

/
/
gm

Unit Tests

given the string . . assert that regex does not match
given the string .. x. assert that regex does not match
given the string .x .. assert that regex does not match
given the string ... ... assert that regex does not match
given the string ..x x.. assert that regex does not match
given the string .... ..x. assert that regex does not match
given the string .x...x... ...x...x. assert that regex does not match
given the string ...xx.....x xx.....xx.. assert that regex does not match
given the string .x x. assert that regex does match
given the string .x. .x. assert that regex does match
given the string .xx xx. assert that regex does match
given the string .xxx. ..x.. assert that regex does match
given the string ..x. xxx. assert that regex does match
given the string .xx.x.. ..xx... assert that regex does match
given the string .....x.x. xxx.x.... assert that regex does match
given the string ....xx.xxx .....xxx.. assert that regex does match
additional unsolvable maze test case