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
/
(youtu(?:\.be|be\.com)\/(?:.*v(?:\/|=)|(?:.*\/)?)([\w'-]+))
/
ig
1st Capturing Group
(youtu(?:\.be|be\.com)\/(?:.*v(?:\/|=)|(?:.*\/)?)([\w'-]+))
youtu
matches the characters youtu literally (case insensitive)
Non-capturing group
(?:\.be|be\.com)
1st Alternative
\.be
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
be
matches the characters be literally (case insensitive)
2nd Alternative
be\.com
be
matches the characters be literally (case insensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
com
matches the characters com literally (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
Non-capturing group
(?:.*v(?:\/|=)|(?:.*\/)?)
1st Alternative
.*v(?:\/|=)
.
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)
v matches the character v with index 11810 (7616 or 1668) literally (case insensitive)
Non-capturing group
(?:\/|=)
1st Alternative
\/
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
2nd Alternative
=
= matches the character = with index 6110 (3D16 or 758) literally (case insensitive)
2nd Alternative
(?:.*\/)?
Non-capturing group
(?:.*\/)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
.
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)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
2nd Capturing Group
([\w'-]+)
Match a single character present in the list below
[\w'-]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
' matches the character ' with index 3910 (2716 or 478) literally (case insensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
This hyphen is treated literally, which might be confusing for others. Consider escaping it or placing at the start or end of the class!
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