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
"
^(http[s]?:\/\/)?([^:\/\s]+)(:([^\/]*))?(\/\w+\.)*([^#?\s]+)(\?([^#]*))?(#(.*))?$
"
i
^ asserts position at start of the string
1st Capturing Group
(http[s]?:\/\/)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
http
matches the characters http literally (case insensitive)
Match a single character present in the list below
[s]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
s matches the character s with index 11510 (7316 or 1638) literally (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 character / with index 4710 (2F16 or 578) literally (case insensitive)
2nd Capturing Group
([^:\/\s]+)
Match a single character not present in the list below
[^:\/\s]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
: matches the character : with index 5810 (3A16 or 728) literally (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v  ])
3rd 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 5810 (3A16 or 728) 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 4710 (2F16 or 578) literally (case insensitive)
5th Capturing Group
(\/\w+\.)*
* 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 4710 (2F16 or 578) literally (case insensitive)
\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)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
6th Capturing Group
([^#?\s]+)
Match a single character not present in the list below
[^#?\s]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
#?
matches a single character in the list #? (case insensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v  ])
7th 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 6310 (3F16 or 778) literally (case insensitive)
8th 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 3510 (2316 or 438) literally (case insensitive)
9th 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 3510 (2316 or 438) literally (case insensitive)
10th Capturing Group
(.*)
.
matches any character (except for line terminators)
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
Global pattern flags
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"
"
i

Test String