Skip to content

Instantly share code, notes, and snippets.

@FiloSottile
Created March 24, 2012 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FiloSottile/2176911 to your computer and use it in GitHub Desktop.
Save FiloSottile/2176911 to your computer and use it in GitHub Desktop.
Youtube video URL regex from `youtube-dl` explained
# The youtube-dl YouTube URL regex

^
(
	(?:https?://)?										# http(s):// (optional)
	(?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/|tube\.majestyc\.net/) # the various hostnames, with wildcard subdomains
	(?!view_play_list|my_playlists|artist|playlist)		# ignore playlist URLs
	(?:													# the various things that can precede the ID:
		(?:(?:v|embed|e)/)								# v/ or embed/ or e/
		|(?:											# or the v= param in all its forms
			(?:watch(?:_popup)?(?:\.php)?)?				# preceding watch(_popup|.php) or nothing (like /?v=xxxx)
			(?:\?|#!?)									# the params delimiter ? or # or #!
			(?:.+&)?									# any other preceding param (like /?s=tuff&v=xxxx)
			v=
		)
	)?													# optional -> youtube.com/xxxx is OK
)?														# all until now is optional -> you can pass the naked ID
([0-9A-Za-z_-]+)										# here is it! the YouTube video ID
(?(1).+)?												# if we found the ID, everything can follow
$

# Glossary: (?:) is a non-tracking group
#			^ is the beginning of the line and $ the end
#			(?!) negative lookahead i.e. match if NOT found
#			(?(1)) conditional meaning "if the first group has matched"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment