0

I have a feed that imports youtube video links within my pipe, however naturally youtube links are displayed in various formats...

http://youtube.com/onmdclU47Us
http://youtu.be/onmdclU47Us
http://www.youtube.com/watch?feature=player_detailpage&v=onmdclU47Us

and various other variations with url parameters. I'm trying to parse each link and get an embed link for the video instead. Which basically is using the regex function and swapping it out for a youtube.com/embed/{video_ID} instead.

However this doesn't work on all instances... this is what I've got, any help?

.+watch\?v=([0-9a-zA-Z-]+)

&([a-zA-Z0-9_-]+)?=([a-zA-Z0-9_-]+)?.([a-zA-Z0-9_-]+)?

.watch?feature=player_embedded&v=([0-9a-zA-Z-]+)

http://youtu.be/([0-9a-zA-Z-]+)

Are these right, or am I missing something?

Chris Gwynne
  • 47
  • 2
  • 7
  • 1
    See this [post](http://stackoverflow.com/questions/9594943/regex-pattern-to-get-the-youtube-video-id-from-any-youtube-url?rq=1) for this answer: `(?<=v=)[^&]+` – robinCTS Jan 25 '13 at 00:51
  • 1
    Unless you have dozens of different types of links, and are interested in filtering down to youtube links? Or whatever you're regexing in Doesn't allow lookbehinds. What's your platform/programming language? – FrankieTheKneeMan Jan 25 '13 at 01:02
  • @Frankie - Except the OP specifically mentioned he only had youtube links. Good point about the lookbehinds though. Just realized as well - this answer doesn't match the first two examples!. @ Chris - please add a tag specifying how you plan to implement the regex. – robinCTS Jan 25 '13 at 06:15

1 Answers1

0

Previous Answer Deleted


I just spent well over an hour looking at a lot of the "Extract YouTube ID" posts on SO. I think I have come up with an almost bulletproof regex to extract the id which is nothing like what I found. It works with thumb URLs as well.

First of all the caveats:

  1. The URL is assumed to be a valid youtube one.
  2. Actually, that's it. There isn't another.

Here goes then:

(?:/favicon|%3D|[/=])([\w-]{11})[^\w-]

That's it. Very simple. And look - no lookarounds.

Notes:

  1. The ID is returned in capturing group 1.
  2. Shouldn't be too hard to tack a regex to the front to actually detect youtube urls.
robinCTS
  • 5,746
  • 14
  • 30
  • 37