Regular Expressions 101

Save & Share

  • Save Regex
    ctrl+s
  • Update Regex
    ctrl+⇧+s

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8

Function

  • Match
  • Substitution
  • List
  • Unit Tests
/
^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$
/
img
^ asserts position at start of a line
Non-capturing group
(?:(?:https?|ftp):\/\/)
Non-capturing group
(?:https?|ftp)
1st Alternative
https?
http
matches the characters http literally (case insensitive)
s
matches the character s with index 11510 (7316 or 1638) literally (case insensitive)
2nd Alternative
ftp
ftp
matches the characters ftp 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)
Non-capturing group
(?:\S+(?::\S*)?@)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\S
matches any non-whitespace character (equivalent to [^\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Non-capturing group
(?::\S*)?
? 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)
\S
matches any non-whitespace character (equivalent to [^\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
@ matches the character @ with index 6410 (4016 or 1008) literally (case insensitive)
Non-capturing group
(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)
1st Alternative
(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))
Negative Lookahead
(?!(?:10|127)(?:\.\d{1,3}){3})
Assert that the Regex below does not match
Non-capturing group
(?:10|127)
1st Alternative
10
10
matches the characters 10 literally (case insensitive)
2nd Alternative
127
127
matches the characters 127 literally (case insensitive)
Non-capturing group
(?:\.\d{1,3}){3}
{3} matches the previous token exactly 3 times
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
\d
matches a digit (equivalent to [0-9])
{1,3} matches the previous token between 1 and 3 times, as many times as possible, giving back as needed (greedy)
Negative Lookahead
(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})
Assert that the Regex below does not match
Non-capturing group
(?:169\.254|192\.168)
1st Alternative
169\.254
169
matches the characters 169 literally (case insensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
254
matches the characters 254 literally (case insensitive)
2nd Alternative
192\.168
192
matches the characters 192 literally (case insensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
168
matches the characters 168 literally (case insensitive)
Non-capturing group
(?:\.\d{1,3}){2}
{2} matches the previous token exactly 2 times
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
\d
matches a digit (equivalent to [0-9])
{1,3} matches the previous token between 1 and 3 times, as many times as possible, giving back as needed (greedy)
Negative Lookahead
(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})
Assert that the Regex below does not match
172
matches the characters 172 literally (case insensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
Non-capturing group
(?:1[6-9]|2\d|3[0-1])
1st Alternative
1[6-9]
1 matches the character 1 with index 4910 (3116 or 618) literally (case insensitive)
Match a single character present in the list below
[6-9]
6-9 matches a single character in the range between 6 (index 54) and 9 (index 57) (case insensitive)
2nd Alternative
2\d
2 matches the character 2 with index 5010 (3216 or 628) literally (case insensitive)
\d matches a digit (equivalent to [0-9])
3rd Alternative
3[0-1]
3 matches the character 3 with index 5110 (3316 or 638) literally (case insensitive)
Match a single character present in the list below
[0-1]
0-1 matches a single character in the range between 0 (index 48) and 1 (index 49) (case insensitive)
Non-capturing group
(?:\.\d{1,3}){2}
{2} matches the previous token exactly 2 times
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
\d
matches a digit (equivalent to [0-9])
{1,3} matches the previous token between 1 and 3 times, as many times as possible, giving back as needed (greedy)
Non-capturing group
(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])
1st Alternative
[1-9]\d?
Match a single character present in the list below
[1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case insensitive)
\d
matches a digit (equivalent to [0-9])
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
2nd Alternative
1\d\d
1 matches the character 1 with index 4910 (3116 or 618) literally (case insensitive)
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
3rd Alternative
2[01]\d
2 matches the character 2 with index 5010 (3216 or 628) literally (case insensitive)
Match a single character present in the list below
[01]
01
matches a single character in the list 01 (case insensitive)
\d matches a digit (equivalent to [0-9])
4th Alternative
22[0-3]
22
matches the characters 22 literally (case insensitive)
Match a single character present in the list below
[0-3]
0-3 matches a single character in the range between 0 (index 48) and 3 (index 51) (case insensitive)
Non-capturing group
(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}
{2} matches the previous token exactly 2 times
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
Non-capturing group
(?:1?\d{1,2}|2[0-4]\d|25[0-5])
1st Alternative
1?\d{1,2}
1
matches the character 1 with index 4910 (3116 or 618) literally (case insensitive)
\d
matches a digit (equivalent to [0-9])
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
2nd Alternative
2[0-4]\d
2 matches the character 2 with index 5010 (3216 or 628) literally (case insensitive)
Match a single character present in the list below
[0-4]
0-4 matches a single character in the range between 0 (index 48) and 4 (index 52) (case insensitive)
\d matches a digit (equivalent to [0-9])
3rd Alternative
25[0-5]
25
matches the characters 25 literally (case insensitive)
Match a single character present in the list below
[0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (case insensitive)
Non-capturing group
(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
Non-capturing group
(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4])
1st Alternative
[1-9]\d?
Match a single character present in the list below
[1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case insensitive)
\d
matches a digit (equivalent to [0-9])
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
2nd Alternative
1\d\d
1 matches the character 1 with index 4910 (3116 or 618) literally (case insensitive)
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
3rd Alternative
2[0-4]\d
2 matches the character 2 with index 5010 (3216 or 628) literally (case insensitive)
Match a single character present in the list below
[0-4]
0-4 matches a single character in the range between 0 (index 48) and 4 (index 52) (case insensitive)
\d matches a digit (equivalent to [0-9])
4th Alternative
25[0-4]
25
matches the characters 25 literally (case insensitive)
Match a single character present in the list below
[0-4]
2nd Alternative
(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?
Non-capturing group
(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)
Non-capturing group
(?:[a-z\u00a1-\uffff0-9]-*)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[a-z\u00a1-\uffff0-9]
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
\u00a1-\uffff matches a single character in the range between ¡ (index 161) and ￿ (index 65535) (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)
Match a single character present in the list below
[a-z\u00a1-\uffff0-9]
+ 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)
\u00a1-\uffff matches a single character in the range between ¡ (index 161) and ￿ (index 65535) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
Non-capturing group
(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*
* matches the previous token between zero 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)
Non-capturing group
(?:[a-z\u00a1-\uffff0-9]-*)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[a-z\u00a1-\uffff0-9]
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
\u00a1-\uffff matches a single character in the range between ¡ (index 161) and ￿ (index 65535) (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)
Match a single character present in the list below
[a-z\u00a1-\uffff0-9]
+ 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)
\u00a1-\uffff matches a single character in the range between ¡ (index 161) and ￿ (index 65535) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
Non-capturing group
(?:\.(?:[a-z\u00a1-\uffff]{2,}))
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
Non-capturing group
(?:[a-z\u00a1-\uffff]{2,})
Match a single character present in the list below
[a-z\u00a1-\uffff]
{2,} matches the previous token between 2 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)
\u00a1-\uffff matches a single character in the range between ¡ (index 161) and ￿ (index 65535) (case insensitive)
\.
matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Non-capturing group
(?::\d{2,5})?
? 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)
\d
matches a digit (equivalent to [0-9])
{2,5} matches the previous token between 2 and 5 times, as many times as possible, giving back as needed (greedy)
Non-capturing group
(?:[/?#]\S*)?
? 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
[/?#]
/?#
matches a single character in the list /?# (case insensitive)
\S
matches any non-whitespace character (equivalent to [^\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
* 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 a line
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
g modifier: global. All matches (don't return after first match)
Your regular expression does not match the subject string.

Regular Expression
No Match

/
/
img

Test String