如何忽略"& feature = xxxxxxxxx"从YouTube网址获取视频ID

时间:2014-07-08 07:24:31

标签: android youtube

这是我的正则表达式:

"(?:http|https|)(?::\\/\\/|)(?:www.|m.)(?:youtu\\.be\\/|youtube\\.com(?:\\/embed\\/|\\/v\\/|\\/watch\\?v=|\\/ytscreeningroom\\?v=|\\/feeds\\/api\\/videos\\/|\\/user\\S*[^\\w\\-\\s]|\\S*[^\\w\\-\\s]))([\\w\\-]{11})[a-z0-9;:@?&%=+\\/\\$_.-]*"

无法从此网址获取ID:

http://www.youtube.com/watch?v=AfG82u8xEKo&feature=youtube_gdata_player9

2 个答案:

答案 0 :(得分:1)

尝试这就像魅力一样

public String getVideoId(String videoUrl) {

        String video_id = "";
        if (videoUrl != null && videoUrl.trim().length() > 0) {
            String s = "^.*(?:youtu.be\\/|v\\/|e\\/|u\\/\\w+\\/|embed\\/|v=)([^#\\&\\?]*).*";
            CharSequence input = videoUrl;
            Pattern pattern = Pattern.compile(s, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(input);
            if (matcher.matches()) {
                System.out.println("DATA" + matcher.group(1));
                String groupIndex1 = matcher.group(1);
                if (groupIndex1 != null && groupIndex1.length() == 11)
                    video_id = groupIndex1;
            }
        }

        System.out.println("VIDEOID" + video_id);
        return video_id;
    }

答案 1 :(得分:0)

只需拆分字符串。

String [] id = url.split("&");