I\'ve written a ruby youtube url parser. It\'s designed to take an input of a youtube url of one of the following structures (these are currently the youtube url structures tha
def parse_youtube url
regex = /(?:.be\/|\/watch\?v=|\/(?=p\/))([\w\/\-]+)/
url.match(regex)[1]
end
urls = %w[http://youtu.be/sGE4HMvDe-Q
http://www.youtube.com/watch?v=Lp7E973zozc&feature=relmfu
http://www.youtube.com/p/A0C3C1D163BE880A?hl=en_US&fs=1]
urls.each {|url| puts parse_youtube url }
# sGE4HMvDe-Q
# Lp7E973zozc
# p/A0C3C1D163BE880A
Depending on how you use this, you might want a better validation that the URL is indeed from youtube.
UPDATE:
Coming back to this a few years later. I've always been annoyed by how sloppy the original answer was. Since the validity of the Youtube domain wasn't validated anyway, I've removed some of the slop.
NODE EXPLANATION
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
be 'be'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
watch 'watch'
--------------------------------------------------------------------------------
\? '?'
--------------------------------------------------------------------------------
v= 'v='
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
p 'p'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[\w\/\-]+ any character of: word characters (a-z,
A-Z, 0-9, _), '\/', '\-' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1