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 (3)
/
^([a-z]+\:)\/*([^\/\s\?]*)(\?[a-z0-9\-@\^=%&;\/~\+]*)*[\?]?([^ \#]*)#?([^ \#]*)
/
ig
^ asserts position at start of the string
1st Capturing Group
([a-z]+\:)
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 insensitive)
\: matches the character : with index 5810 (3A16 or 728) literally (case insensitive)
\/
matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group
([^\/\s\?]*)
Match a single character not present in the list below
[^\/\s\?]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
\? matches the character ? with index 6310 (3F16 or 778) literally (case insensitive)
3rd Capturing Group
(\?[a-z0-9\-@\^=%&;\/~\+]*)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
\? matches the character ? with index 6310 (3F16 or 778) literally (case insensitive)
Match a single character present in the list below
[a-z0-9\-@\^=%&;\/~\+]
* matches the previous token between zero 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 insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
\- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
@ matches the character @ with index 6410 (4016 or 1008) literally (case insensitive)
\^ matches the character ^ with index 9410 (5E16 or 1368) literally (case insensitive)
=%&;
matches a single character in the list =%&; (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
~ matches the character ~ with index 12610 (7E16 or 1768) literally (case insensitive)
\+ matches the character + with index 4310 (2B16 or 538) 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 the character ? with index 6310 (3F16 or 778) literally (case insensitive)
4th Capturing Group
([^ \#]*)
Match a single character not present in the list below
[^ \#]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case insensitive)
\# matches the character # with index 3510 (2316 or 438) literally (case insensitive)
#
matches the character # with index 3510 (2316 or 438) literally (case insensitive)
5th Capturing Group
([^ \#]*)
Match a single character not present in the list below
[^ \#]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case insensitive)
\# matches the character # with index 3510 (2316 or 438) literally (case insensitive)
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
g modifier: global. All matches (don't return after first match)
Your regular expression does not match the subject string.

Regular Expression
No Match

/
/
ig

Test String