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<user>^[a-zA-Z]+[\w+.-]*\@)(?P<domain>[\w+.-]*[a-zA-Z]$)
"
g
Named Capture Group user
(?P<user>^[a-zA-Z]+[\w+.-]*\@)
^ asserts position at start of the string
Match a single character present in the list below
[a-zA-Z]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
Match a single character present in the list below
[\w+.-]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
+.-
matches a single character in the list +.- (case sensitive)
\@ matches the character @ with index 6410 (4016 or 1008) literally (case sensitive)
Named Capture Group domain
(?P<domain>[\w+.-]*[a-zA-Z]$)
Match a single character present in the list below
[\w+.-]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
+.-
matches a single character in the list +.- (case sensitive)
Match a single character present in the list below
[a-zA-Z]
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
Global pattern flags
g modifier: global. All matches (don't return after first match)
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
g

Test String