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<ipc_cpc_fi>\b(?P<section>[A-HY])(?(section)(?P<class>\d{2})?)(?(class)(?P<subclass>[A-Z])?)(?(subclass) *(?P<group>\d{1,})?)(?(group)(?:/(?P<subgroup>\d{1,}))?)(?(subgroup) *(?P<subsubgroup>[A-Z]?))\b)
"
gm
Named Capture Group ipc_cpc_fi
(?P<ipc_cpc_fi>\b(?P<section>[A-HY])(?(section)(?P<class>\d{2})?)(?(class)(?P<subclass>[A-Z])?)(?(subclass) *(?P<group>\d{1,})?)(?(group)(?:/(?P<subgroup>\d{1,}))?)(?(subgroup) *(?P<subsubgroup>[A-Z]?))\b)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
Named Capture Group section
(?P<section>[A-HY])
Match a single character present in the list below
[A-HY]
A-H matches a single character in the range between A (index 65) and H (index 72) (case sensitive)
Y matches the character Y with index 8910 (5916 or 1318) literally (case sensitive)
Conditional
(?(section)(?P<class>\d{2})?)
Conditionally matches one of two options depending on whether the section capturing group matched
If condition is met, match the following regex
(?P<class>\d{2})?
Named Capture Group class
(?P<class>\d{2})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d
matches a digit (equivalent to [0-9])
{2} matches the previous token exactly 2 times
Conditional
(?(class)(?P<subclass>[A-Z])?)
Conditionally matches one of two options depending on whether the class capturing group matched
If condition is met, match the following regex
(?P<subclass>[A-Z])?
Named Capture Group subclass
(?P<subclass>[A-Z])?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[A-Z]
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
Conditional
(?(subclass) *(?P<group>\d{1,})?)
Conditionally matches one of two options depending on whether the subclass capturing group matched
If condition is met, match the following regex
*(?P<group>\d{1,})?
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Named Capture Group group
(?P<group>\d{1,})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d
matches a digit (equivalent to [0-9])
{1,} matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Conditional
(?(group)(?:/(?P<subgroup>\d{1,}))?)
Conditionally matches one of two options depending on whether the group capturing group matched
If condition is met, match the following regex
(?:/(?P<subgroup>\d{1,}))?
Non-capturing group
(?:/(?P<subgroup>\d{1,}))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Named Capture Group subgroup
(?P<subgroup>\d{1,})
\d
matches a digit (equivalent to [0-9])
{1,} matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Conditional
(?(subgroup) *(?P<subsubgroup>[A-Z]?))
Conditionally matches one of two options depending on whether the subgroup capturing group matched
If condition is met, match the following regex
*(?P<subsubgroup>[A-Z]?)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Named Capture Group subsubgroup
(?P<subsubgroup>[A-Z]?)
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)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
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

r"
"
gm

Test String

Substitution

Processing...