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 (10)
"
^ ( # Match one of these: [A-Z]?[a-z]+| # A possibly-capitalized identifier, or: [A-Z]+ # An ALL CAPS identifier... (?![a-z]) # ...not immediately followed by lowercase ) # and capture the rest of the string (.*?) $
"
xgm
^ asserts position at start of a line
1st Capturing Group
( # Match one of these: [A-Z]?[a-z]+| # A possibly-capitalized identifier, or: [A-Z]+ # An ALL CAPS identifier... (?![a-z]) # ...not immediately followed by lowercase )
1st Alternative
# Match one of these: [A-Z]?[a-z]+
# Match one of these:
Match one of these:
Comment: Match one of these:
Match a single character present in the list below
[A-Z]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
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
[a-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)
2nd Alternative
# A possibly-capitalized identifier, or: [A-Z]+ # An ALL CAPS identifier... (?![a-z]) # ...not immediately followed by lowercase
# A possibly-capitalized identifier, or:
A possibly-capitalized identifier, or:
Comment: A possibly-capitalized identifier, or:
Match a single character present in the list below
[A-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 65) and Z (index 90) (case sensitive)
# An ALL CAPS identifier...
An ALL CAPS identifier...
Comment: An ALL CAPS identifier...
Negative Lookahead
(?![a-z])
Assert that the Regex below does not match
Match a single character present in the list below
[a-z]
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
# ...not immediately followed by lowercase
...not immediately followed by lowercase
Comment: ...not immediately followed by lowercase
# and capture the rest of the string
and capture the rest of the string
Comment: and capture the rest of the string
2nd 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)
$ asserts position at the end of a line
Global pattern flags
x modifier: extended. Spaces and text after a # in the pattern are ignored
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

r"
"
xgm

Unit Tests

Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test