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|https)://)?((?:[A--ЯЁ0-9](?:[A--ЯЁ0-9-]{0,61}[A--ЯЁ0-9])?\.)+(?:[A--ЯЁ]{2,6}\.?|[A--ЯЁ0-9-]{2,}(?<!-)\.?))(?:/?|[/?]\S+)$
"
iu
^ asserts position at start of the string
Non-capturing group
(?:(?:http|https)://)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Non-capturing group
(?:http|https)
1st Alternative
http
http
matches the characters http literally (case insensitive)
2nd Alternative
https
https
matches the characters https literally (case insensitive)
://
matches the characters :// literally (case insensitive)
1st Capturing Group
((?:[A--ЯЁ0-9](?:[A--ЯЁ0-9-]{0,61}[A--ЯЁ0-9])?\.)+(?:[A--ЯЁ]{2,6}\.?|[A--ЯЁ0-9-]{2,}(?<!-)\.?))
Non-capturing group
(?:[A--ЯЁ0-9](?:[A--ЯЁ0-9-]{0,61}[A--ЯЁ0-9])?\.)+
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[A--ЯЁ0-9]
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
А-Я matches a single character in the range between А (index 1040) and Я (index 1071) (case insensitive)
Ё matches the character Ё with index 102510 (40116 or 20018) literally (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--ЯЁ0-9-]{0,61}[A--ЯЁ0-9])?
? 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--ЯЁ0-9-]
{0,61} matches the previous token between 0 and 61 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 insensitive)
А-Я matches a single character in the range between А (index 1040) and Я (index 1071) (case insensitive)
Ё matches the character Ё with index 102510 (40116 or 20018) literally (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--ЯЁ0-9]
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
А-Я matches a single character in the range between А (index 1040) and Я (index 1071) (case insensitive)
Ё matches the character Ё with index 102510 (40116 or 20018) literally (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 4610 (2E16 or 568) literally (case insensitive)
Non-capturing group
(?:[A--ЯЁ]{2,6}\.?|[A--ЯЁ0-9-]{2,}(?<!-)\.?)
1st Alternative
[A--ЯЁ]{2,6}\.?
Match a single character present in the list below
[A--ЯЁ]
{2,6} matches the previous token between 2 and 6 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 insensitive)
А-Я matches a single character in the range between А (index 1040) and Я (index 1071) (case insensitive)
Ё matches the character Ё with index 102510 (40116 or 20018) literally (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)
2nd Alternative
[A--ЯЁ0-9-]{2,}(?<!-)\.?
Match a single character present in the list below
[A--ЯЁ0-9-]
{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 65) and Z (index 90) (case insensitive)
А-Я matches a single character in the range between А (index 1040) and Я (index 1071) (case insensitive)
Ё matches the character Ё with index 102510 (40116 or 20018) literally (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)
Negative Lookbehind
(?<!-)
Assert that the Regex below does not match
- matches the character - with index 4510 (2D16 or 558) literally (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
(?:/?|[/?]\S+)
1st Alternative
/?
/
matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
2nd Alternative
[/?]\S+
Match a single character present in the list below
[/?]
/?
matches a single character in the list /? (case insensitive)
\S
matches any kind of visible character (equivalent to [^\p{Z}\h\v])
+ matches the previous token between one 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])
u modifier: unicode. Make \w, \W, \b, \B, \d, \D, \s and \S perform matching with Unicode semantic (redundant in Python 3)
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
iu

Test String