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 (2)
/
^https:\/\/(youtu\.be\/[A-Za-z0-9]{11}|www\.youtube\.com\/watch\?v=[A-Za-z0-9]{11})$
/
^ asserts position at start of the string
https:
matches the characters https: literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
1st Capturing Group
(youtu\.be\/[A-Za-z0-9]{11}|www\.youtube\.com\/watch\?v=[A-Za-z0-9]{11})
1st Alternative
youtu\.be\/[A-Za-z0-9]{11}
youtu
matches the characters youtu literally (case sensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
be
matches the characters be literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Match a single character present in the list below
[A-Za-z0-9]
{11} matches the previous token exactly 11 times
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Alternative
www\.youtube\.com\/watch\?v=[A-Za-z0-9]{11}
www
matches the characters www literally (case sensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
youtube
matches the characters youtube literally (case sensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
com
matches the characters com literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
watch
matches the characters watch literally (case sensitive)
\? matches the character ? with index 6310 (3F16 or 778) literally (case sensitive)
v=
matches the characters v= literally (case sensitive)
Match a single character present in the list below
[A-Za-z0-9]
{11} matches the previous token exactly 11 times
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

Regular Expression

/
/

Unit Tests

given the string https://www.youtube.com/watch?v=jNQXAC9I… assert that regex does match
given the string https://youtu.be/jNQXAC9IVRw assert that regex does match