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
"
# Keys are pretty much "any text before the '=' symbol" (?P<key>[^=]+) # Match the '=' symbol = (?P<value> # Lazy match .*? # Look-ahead (don't move the "regex cursor", just "peek forward") (?= # Match a '_' that has no other '_' between it and the '=' character, or... _(?=[^_=\n]+=) | # ... or the end of the line $ ) ) # Actually "consume" the '_' character if it exists (or just match the end-of-line) (?:_|$)
"
gmx
Comment: Keys are pretty much "any text before the '=' symbol"
Named Capture Group key
(?P<key>[^=]+)
Match a single character not present in the list below
[^=]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
= matches the character = with index 6110 (3D16 or 758) literally (case sensitive)
# Match the '=' symbol =
Match the '=' symbol
Comment: Match the '=' symbol
= matches the character = with index 6110 (3D16 or 758) literally (case sensitive)
Named Capture Group value
(?P<value> # Lazy match .*? # Look-ahead (don't move the "regex cursor", just "peek forward") (?= # Match a '_' that has no other '_' between it and the '=' character, or... _(?=[^_=\n]+=) | # ... or the end of the line $ ) )
# Lazy match
Lazy match
Comment: Lazy match
.
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)
# Look-ahead (don't move the "regex cursor", just "peek forward")
Look-ahead (don't move the "regex cursor", just "peek forward")
Comment: Look-ahead (don't move the "regex cursor", just "peek forward")
Positive Lookahead
(?= # Match a '_' that has no other '_' between it and the '=' character, or... _(?=[^_=\n]+=) | # ... or the end of the line $ )
Assert that the Regex below matches
1st Alternative
# Match a '_' that has no other '_' between it and the '=' character, or... _(?=[^_=\n]+=)
# Match a '_' that has no other '_' between it and the '=' character, or... _
Match a '_' that has no other '_' between it and the '=' character, or...
Comment: Match a '_' that has no other '_' between it and the '=' character, or...
_ matches the character _ with index 9510 (5F16 or 1378) literally (case sensitive)
Positive Lookahead
(?=[^_=\n]+=)
Assert that the Regex below matches
Match a single character not present in the list below
[^_=\n]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
_=
matches a single character in the list _= (case sensitive)
\n matches a line-feed (newline) character (ASCII 10)
= matches the character = with index 6110 (3D16 or 758) literally (case sensitive)
2nd Alternative
# ... or the end of the line $
# ... or the end of the line
... or the end of the line
Comment: ... or the end of the line
$ asserts position at the end of a line
# Actually "consume" the '_' character if it exists (or just match the end-of-line)
Actually "consume" the '_' character if it exists (or just match the end-of-line)
Comment: Actually "consume" the '_' character if it exists (or just match the end-of-line)
Non-capturing group
(?:_|$)
1st Alternative
_
_ matches the character _ with index 9510 (5F16 or 1378) literally (case sensitive)
2nd Alternative
$
$ asserts position at the end of a line
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)
x modifier: extended. Spaces and text after a # in the pattern are ignored
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
gmx

Test String

Substitution

Processing...