从链接获取 youtube id [英] getting the youtube id from a link

查看:84
本文介绍了从链接获取 youtube id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个代码是为了从链接中获取 youtube idwww.youtube.com/watch?v=xxxxxxx

I got this code to get the youtube id from the links like www.youtube.com/watch?v=xxxxxxx

  URL youtubeURL = new URL(link);
  youtubeURL.getQuery();

基本上这会让我很容易得到 id v=xxxxxxxx

basically this will get me the id easily v=xxxxxxxx

但我注意到有时youtube链接会像这样

but I noticed sometime youtube links will be like this

http://gdata.youtube.com/feeds/api/videos/xxxxxx

我从提要中获取链接那么我需要为此构建一个正则表达式还是有一个解析器来为我获取它?

I am getting the links from a feed so do I need to build a regex for that or theres a parser to get that for me ?

推荐答案

尝试了其他的但在我的情况下失败了 - 调整了正则表达式以适合我的网址

Tried the other ones but failed in my case - adjusted the regex to fit for my urls

String pattern = "(?<=watch\?v=|/videos/|embed\/)[^#\&\?]*";
    
    Pattern compiledPattern = Pattern.compile(pattern);
    Matcher matcher = compiledPattern.matcher(url);

    if(matcher.find()){
        return matcher.group();
    }

这适用于:(您也可以实施安全检查 youtubeid length = 11 )

This one works for: (you could also implement a security check youtubeid length = 11 )

http://www.youtube.com/embed/Woq5iX9XQhA?html5=1

http://www.youtube.com/watch?v=384IUU43bfQ

http://gdata.youtube.com/feeds/api/视频/xTmi7zzUa-M&whatever

Woq5iX9XQhA

Woq5iX9XQhA

384IUU43bfQ

384IUU43bfQ

xTmi7zzUa-M

xTmi7zzUa-M

这篇关于从链接获取 youtube id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆