Regular Expressions 101

Save & Share

Flavor

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

Function

  • Match
  • Substitution
  • List
  • Unit Tests
/
(?=.*(START))(?=.*(END))(?:(?:\1|\2)|(?!^)\G)\h+.*?\b(\w{4,})(?=.*?(?:(?:\1|\2)))
/
g
Positive Lookahead
(?=.*(START))
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)
1st Capturing Group
(START)
START
matches the characters START literally (case sensitive)
Positive Lookahead
(?=.*(END))
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)
2nd Capturing Group
(END)
END
matches the characters END literally (case sensitive)
Non-capturing group
(?:(?:\1|\2)|(?!^)\G)
1st Alternative
(?:\1|\2)
Non-capturing group
(?:\1|\2)
1st Alternative
\1
\1 matches the same text as most recently matched by the 1st capturing group
2nd Alternative
\2
\2 matches the same text as most recently matched by the 2nd capturing group
2nd Alternative
(?!^)\G
Negative Lookahead
(?!^)
Assert that the Regex below does not match
^ asserts position at start of the string
\G asserts position at the end of the previous match or the start of the string for the first match
\h
matches any horizontal whitespace character (equivalent to [[:blank:]])
+ matches the previous token between one and unlimited 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 unlimited times, as few times as possible, expanding as needed (lazy)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
3rd Capturing Group
(\w{4,})
\w
matches any word character (equivalent to [a-zA-Z0-9_])
{4,} matches the previous token between 4 and unlimited times, as many times as possible, giving back as needed (greedy)
Positive Lookahead
(?=.*?(?:(?:\1|\2)))
Assert that the Regex below matches
.
matches any character (except for line terminators)
*? matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
Non-capturing group
(?:(?:\1|\2))
Non-capturing group
(?:\1|\2)
1st Alternative
\1
\1 matches the same text as most recently matched by the 1st capturing group
2nd Alternative
\2
\2 matches the same text as most recently matched by the 2nd capturing group
Global pattern flags
g modifier: global. All matches (don't return after first match)
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
g

Test String