5

Example URLs

http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel
http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub
http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I

Any regex that will pull the correct YID from all 4 of these use cases? The first case is especially odd.

Thank you.

putorti
  • 353
  • 5
  • 8
  • 1
    Look here: http://stackoverflow.com/questions/tagged/youtube+regex – codaddict Apr 08 '10 at 02:01
  • As far as I can tell, nobody else has handled my first test case. – putorti Apr 08 '10 at 02:19
  • Use them as examples and adapt. It's not *that* difficult. – ceejayoz Apr 08 '10 at 02:44
  • I've updated [my regex answer](http://stackoverflow.com/questions/5830387/php-regex-find-all-youtube-video-ids-in-string/5831191#5831191) over on the: "[php regex - find all youtube video ids in string](http://stackoverflow.com/questions/5830387/php-regex-find-all-youtube-video-ids-in-string)" question to handle these (new to me) YouTube URL syntaxes. – ridgerunner Jul 27 '11 at 23:34

2 Answers2

14
(?<=v=)[a-zA-Z0-9-_]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+

This works. https://i.stack.imgur.com/J7yXK.jpg

webnat0
  • 2,646
  • 5
  • 29
  • 43
6

For what it's worth, this worked on http://rubular.com/

(v=|\/)([\w-]+)(&.+)?$

Grabbing second capture group for these:

http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY
http://youtu.be/6dwqZw0j_jY
http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be
http://youtu.be/afa-5HQHiAs
http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel
http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub
http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I

(the editor made me tab the URLs in as code, sorry)

Joe Sak
  • 770
  • 6
  • 13
  • 2
    Best regexp I've seen. You only have to check for match group 2 but it's pretty neat. BTW: This returns the id in match group 1 `[v=|\/]([\w-]+)(&.+)?$` – Phrozen Nov 06 '12 at 02:31