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
"
(?P<lname>(?<=^)(?:.*?)(?=, )).*(?P<fname>(?<=, )(?:.*?)(?= \<)).*(?P<email>(?<=\<)(?:.*?)(?=\>))
"
Named Capture Group lname
(?P<lname>(?<=^)(?:.*?)(?=, ))
Positive Lookbehind
(?<=^)
Assert that the Regex below matches
^ asserts position at start of the string
Non-capturing group
(?:.*?)
.
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)
Positive Lookahead
(?=, )
Assert that the Regex below matches
,
matches the characters , 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)
Named Capture Group fname
(?P<fname>(?<=, )(?:.*?)(?= \<))
Positive Lookbehind
(?<=, )
Assert that the Regex below matches
,
matches the characters , literally (case sensitive)
Non-capturing group
(?:.*?)
.
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)
Positive Lookahead
(?= \<)
Assert that the Regex below matches
matches the character with index 3210 (2016 or 408) literally (case sensitive)
\< matches the character < with index 6010 (3C16 or 748) 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)
Named Capture Group email
(?P<email>(?<=\<)(?:.*?)(?=\>))
Positive Lookbehind
(?<=\<)
Assert that the Regex below matches
\< matches the character < with index 6010 (3C16 or 748) literally (case sensitive)
Non-capturing group
(?:.*?)
.
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)
Positive Lookahead
(?=\>)
Assert that the Regex below matches
\> matches the character > with index 6210 (3E16 or 768) literally (case sensitive)
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"

Test String