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
"
league/((?P<league_slug>[\w-]+)(/(?P<season_slug>\d{4}(-\d{4})?))?(/(?P<action_slug>[\w\-\_]+[(-|_)]?[\w]+))?)?(/)?
"
gmi
league/
matches the characters league/ literally (case insensitive)
1st Capturing Group
((?P<league_slug>[\w-]+)(/(?P<season_slug>\d{4}(-\d{4})?))?(/(?P<action_slug>[\w\-\_]+[(-|_)]?[\w]+))?)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Named Capture Group league_slug
(?P<league_slug>[\w-]+)
Match a single character present in the list below
[\w-]
+ matches the previous token between one 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 the character - with index 4510 (2D16 or 558) literally (case insensitive)
3rd Capturing Group
(/(?P<season_slug>\d{4}(-\d{4})?))?
? 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 insensitive)
Named Capture Group season_slug
(?P<season_slug>\d{4}(-\d{4})?)
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
5th Capturing Group
(-\d{4})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
6th Capturing Group
(/(?P<action_slug>[\w\-\_]+[(-|_)]?[\w]+))?
? 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 insensitive)
Named Capture Group action_slug
(?P<action_slug>[\w\-\_]+[(-|_)]?[\w]+)
Match a single character present in the list below
[\w\-\_]
+ matches the previous token between one 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 the character - with index 4510 (2D16 or 558) literally (case insensitive)
\_ matches the character _ with index 9510 (5F16 or 1378) literally (case insensitive)
Match a single character present in the list below
[(-|_)]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
(-| matches a single character in the range between ( (index 40) and | (index 124) (case insensitive)
_)
matches a single character in the list _) (case insensitive)
Match a single character present in the list below
[\w]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
8th Capturing Group
(/)?
? 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 insensitive)
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)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
gmi

Test String