1

I found a great bit of code here to extract the ID from a youtube URL in PHP.

I'm now having syntax issues converting this regex to Perl.

preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $link, $matches);

This matches the following versions, extracting this portion of the URL: g6QrR5Uakeg

youtube.com/v/g6QrR5Uakeg
youtube.com/vi/g6QrR5Uakeg
youtube.com/?v=g6QrR5Uakeg
youtube.com/?vi=g6QrR5Uakeg
youtube.com/watch?v=g6QrR5Uakeg
youtube.com/watch?vi=g6QrR5Uakeg
youtu.be/g6QrR5Uakeg
www.youtube.com/v/g6QrR5Uakeg?feature=autoshare&version=3&autohide=1&autoplay=1
Community
  • 1
  • 1
chrisrth
  • 1,182
  • 3
  • 15
  • 36
  • Youtube video ID's aren't {vidid}, you can't match for that. – melwil May 07 '13 at 16:51
  • If you want help, you're going to have to post examples of what you want, and what works and doesn't work. – Jim Garrison May 07 '13 at 16:53
  • 2
    "Having issues get the syntax to work when converting this regex to Perl". What issues? Syntax errors? Regex errors? We need to see code so we know how to help. – Nate May 07 '13 at 16:53
  • 2
    Personally, I define "great code" as things that do not give me a headache when I look at it. – TLP May 07 '13 at 16:54
  • {vidid} is merely for example, thought that was apparent. I'll edit post with a fake id – chrisrth May 07 '13 at 16:55
  • 3
    Regex is probably not your best bet for this, [URI::Split](http://search.cpan.org/~gaas/URI-1.60/URI/Split.pm) seems like this is the very purpose it's been designed for. – hd1 May 07 '13 at 16:58

1 Answers1

2

Perhaps,

my ($id) = $url =~ m! (?: \bv i? [/=] | be/ ) (\w+) !x;

in english, match word boundary, then letter 'v', followed by 'i' occurring 0 or 1 times. Then match character '/' or '='.

| stands for "or", so if matching has failed by now, try with chars 'be/'.

If anything of the above succeeded, use \w+ to match ID we're looking for. (\w is word class which stands for 0-9 A-Z a-z and _)

mpapec
  • 50,217
  • 8
  • 67
  • 127