I have the following regex from here: https://stackoverflow.com/a/10405818/924999
val regex = """/https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[?=&+%\w-]*/ig;""".r
I’m attempting to extract the video ID from youtube video urls with:
val url = "http://www.youtube.com/watch?v=XrivBjlv6Mw"
url match {
case regex(result) => result
case _ => null
}
However it seems to always return null, is there something I’m missing or need to do differently?
Thanks in advance for any help, much appreciated 🙂
The regex that you have is php-style regex, not java-style – for example, note
/ig;flags at the end.So you’ll just have to edit it a bit:
I tested it on all possible youtube urls, and it works. Example:
And extracting the value:
It’s a pity that java does not allow proper comments in regexps, so I did what I could:
(adapted from @ridgerunner’s answer here: find all youtube video ids in string)