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)?://)?((www|en-es|en-gb|secure|beta|ro|www-origin|en-ca|fr-ca|lt|zh-tw|he|id|ca|mk|lv|ma|tl|hi|ar|bg|vi|th)\.)?twitch.tv/(?!directory|p|user/legal|admin|login|signup|jobs)(?P<channel>\w+)
"
gm
1st Capturing Group
(^http(s)?://)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
^ asserts position at start of a line
http
matches the characters http literally (case sensitive)
2nd Capturing Group
(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 sensitive)
://
matches the characters :// literally (case sensitive)
3rd Capturing Group
((www|en-es|en-gb|secure|beta|ro|www-origin|en-ca|fr-ca|lt|zh-tw|he|id|ca|mk|lv|ma|tl|hi|ar|bg|vi|th)\.)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
4th Capturing Group
(www|en-es|en-gb|secure|beta|ro|www-origin|en-ca|fr-ca|lt|zh-tw|he|id|ca|mk|lv|ma|tl|hi|ar|bg|vi|th)
1st Alternative
www
www
matches the characters www literally (case sensitive)
2nd Alternative
en-es
en-es
matches the characters en-es literally (case sensitive)
3rd Alternative
en-gb
en-gb
matches the characters en-gb literally (case sensitive)
4th Alternative
secure
secure
matches the characters secure literally (case sensitive)
5th Alternative
beta
beta
matches the characters beta literally (case sensitive)
6th Alternative
ro
ro
matches the characters ro literally (case sensitive)
7th Alternative
www-origin
www-origin
matches the characters www-origin literally (case sensitive)
8th Alternative
en-ca
en-ca
matches the characters en-ca literally (case sensitive)
9th Alternative
fr-ca
fr-ca
matches the characters fr-ca literally (case sensitive)
10th Alternative
lt
lt
matches the characters lt literally (case sensitive)
11th Alternative
zh-tw
12th Alternative
he
13th Alternative
id
14th Alternative
ca
15th Alternative
mk
16th Alternative
lv
17th Alternative
ma
18th Alternative
tl
19th Alternative
hi
20th Alternative
ar
21st Alternative
bg
22nd Alternative
vi
23rd Alternative
th
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
twitch
matches the characters twitch literally (case sensitive)
. matches any character (except for line terminators)
tv/
matches the characters tv/ literally (case sensitive)
Negative Lookahead
(?!directory|p|user/legal|admin|login|signup|jobs)
Assert that the Regex below does not match
1st Alternative
directory
directory
matches the characters directory literally (case sensitive)
2nd Alternative
p
p matches the character p with index 11210 (7016 or 1608) literally (case sensitive)
3rd Alternative
user/legal
user/legal
matches the characters user/legal literally (case sensitive)
4th Alternative
admin
admin
matches the characters admin literally (case sensitive)
5th Alternative
login
login
matches the characters login literally (case sensitive)
6th Alternative
signup
signup
matches the characters signup literally (case sensitive)
7th Alternative
jobs
jobs
matches the characters jobs literally (case sensitive)
Named Capture Group channel
(?P<channel>\w+)
\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)
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)
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
gm

Test String