getting the youtube id from a link

前端 未结 9 1200
清歌不尽
清歌不尽 2020-12-24 13:09

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();
         


        
9条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-24 13:31

    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();
        }
    

    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/videos/xTmi7zzUa-M&whatever

    Woq5iX9XQhA

    384IUU43bfQ

    xTmi7zzUa-M

提交回复
热议问题