Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
Python

r"
^(?:https?:\/\/|\/\/)?(?:(?:www\.|m\.)?youtube(?:-nocookie)?\.com\/(?:(?:vi?|e|embed)\/([\w-]{11})|(?:watch|embed|attribution_link)?\?\S*?(?:(?<=\?v=|&v=)|(?<=\?vi=|&vi=))([\w-]{11}))|youtu\.be\/(?:([\w-]{11})(?!\S*v=)|\S*?(?:&v=|\?v=)([\w-]{11})))(?:[^\w-]\S*)?$
"
gm

Description

This regex can match with a valid YouTube link and retrieve the video ID in one of the four capturing groups. Unfortunately, I couldn't manage to use a single capturing group, but there is no difference between all of them.

Submitted by Lyroy TheToad - 3 years ago (Last modified 3 years ago)