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
"
(?<ApWord>((\w+)(')(\w+)))|(?<HyWord>((\w+)(-)(\w+)))|(?<Rest>\w+)
"
gm
1st Alternative
(?<ApWord>((\w+)(')(\w+)))
Named Capture Group ApWord
(?<ApWord>((\w+)(')(\w+)))
2nd Capturing Group
((\w+)(')(\w+))
3rd Capturing Group
(\w+)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
4th Capturing Group
(')
' matches the character ' with index 3910 (2716 or 478) literally (case sensitive)
5th Capturing Group
(\w+)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Alternative
(?<HyWord>((\w+)(-)(\w+)))
Named Capture Group HyWord
(?<HyWord>((\w+)(-)(\w+)))
7th Capturing Group
((\w+)(-)(\w+))
8th Capturing Group
(\w+)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
9th Capturing Group
(-)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
10th Capturing Group
(\w+)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
3rd Alternative
(?<Rest>\w+)
Named Capture Group Rest
(?<Rest>\w+)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
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)
Your regular expression does not match the subject string.

Regular Expression
No Match

"
"
gm

Test String