getting the youtube id from a link

前端 未结 9 1183
清歌不尽
清歌不尽 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:27

    This will work me and simple
    
    public static String getVideoId(@NonNull String videoUrl) {
        String reg = "(?:youtube(?:-nocookie)?\\.com\\/(?:[^\\/\\n\\s]+\\/\\S+\\/|(?:v|e(?:mbed)?)\\/|\\S*?[?&]v=)|youtu\\.be\\/)([a-zA-Z0-9_-]{11})";
        Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(videoUrl);
    
        if (matcher.find())
            return matcher.group(1);
        return null;
    }
    

提交回复
热议问题