从javascript答案移植到Java版本 JavaScript REGEX: How do I get the YouTube video id from a URL?
答案 0 :(得分:36)
认为这个问题不是针对Java的,如果你需要用Java来做这件事
public static String extractYTId(String ytUrl) {
String vId = null;
Pattern pattern = Pattern.compile(
"^https?://.*(?:youtu.be/|v/|u/\\w/|embed/|watch?v=)([^#&?]*).*$",
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(ytUrl);
if (matcher.matches()){
vId = matcher.group(1);
}
return vId;
}
适用于类似(也适用于https://...
)
http://www.youtube.com/watch?v=0zM4nApSvMg&feature=feedrec_grec_index
http://www.youtube.com/user/SomeUser#p/a/u/1/QDK8U-VIH_o
http://www.youtube.com/v/0zM4nApSvMg?fs=1&hl=en_US&rel=0
http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s
http://www.youtube.com/embed/0zM4nApSvMg?rel=0
http://www.youtube.com/watch?v=0zM4nApSvMg
http://youtu.be/0zM4nApSvMg
答案 1 :(得分:15)
以前的答案对我不起作用..这是正确的方法。它适用于www.youtube.com和youtu.be链接。
private String getYouTubeId (String youTubeUrl) {
String pattern = "(?<=youtu.be/|watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(youTubeUrl);
if(matcher.find()){
return matcher.group();
} else {
return "error";
}
}
答案 2 :(得分:4)
对我而言,对于您发布的示例链接,此正则表达式看起来更精确(将ID捕获到组1):
http://(?:www\.)?youtu(?:\.be/|be\.com/(?:watch\?v=|v/|embed/|user/(?:[\w#]+/)+))([^&#?\n]+)
在demo上,查看右下方窗格中的捕获组。但是,对于第二场比赛,并不完全确定这是一个正确的ID,因为它看起来与其他样本不同 - 如果需要可以调整。
在代码中,例如:
Pattern regex = Pattern.compile("http://(?:www\\.)?youtu(?:\\.be/|be\\.com/(?:watch\\?v=|v/|embed/|user/(?:[\\w#]+/)+))([^&#?\n]+)");
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
VideoID = regexMatcher.group(1);
}
答案 3 :(得分:2)
尝试了其他的但在我的情况下失败了 - 调整正则表达式以适合我的网址
public static String extractYoutubeVideoId(String ytUrl) {
String vId = null;
String pattern = "(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(ytUrl);
if(matcher.find()){
vId= matcher.group();
}
return vId;
}
这个适用于以下网址
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
https://www.youtube.com/watch?v=C7RVaSEMXNk
希望它能帮到某个人。感谢
答案 4 :(得分:1)
使用TấnNguyên的正则表达式:
public String getVideoIdFromYoutubeUrl(String url){
String videoId = null;
String regex = "http(?:s)?:\\/\\/(?:m.)?(?:www\\.)?youtu(?:\\.be\\/|be\\.com\\/(?:watch\\?(?:feature=youtu.be\\&)?v=|v\\/|embed\\/|user\\/(?:[\\w#]+\\/)+))([^&#?\\n]+)";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(url);
if(matcher.find()){
videoId = matcher.group(1);
}
return videoId;
}
信贷给TấnNguyên。
答案 5 :(得分:1)
private fun extractVideoId(ytUrl: String?): String? {
var videoId: String? = null
val regex =
"^((?:https?:)?//)?((?:www|m)\\.)?((?:youtube\\.com|youtu.be|youtube-nocookie.com))(/(?:[\\w\\-]+\\?v=|feature=|watch\\?|e/|embed/|v/)?)([\\w\\-]+)(\\S+)?\$"
val pattern: Pattern = Pattern.compile(
regex ,
Pattern.CASE_INSENSITIVE
)
val matcher: Matcher = pattern.matcher(ytUrl)
if (matcher.matches()) {
videoId = matcher.group(5)
}
return videoId
}
上述带有正则表达式的函数可以从中提取视频ID
https://www.youtube.com/watch?v=DFYRQ_zQ-gk&feature=featured
https://www.youtube.com/watch?v=DFYRQ_zQ-gk
http://www.youtube.com/watch?v=DFYRQ_zQ-gk
http://www.youtube.com/watch?v=DFYRQ_zQ-gk
http://www.youtube.com/watch?v=DFYRQ_zQ-gk
https://youtube.com/watch?v=DFYRQ_zQ-gk
http://youtube.com/watch?v=DFYRQ_zQ-gk
https://youtube.com/watch?v=DFYRQ_zQ-gk
http://youtube.com/watch?v=DFYRQ_zQ-gk
https://m.youtube.com/watch?v=DFYRQ_zQ-gk
http://m.youtube.com/watch?v=DFYRQ_zQ-gk
http://m.youtube.com/watch?v=DFYRQ_zQ-gk
http://m.youtube.com/watch?v=DFYRQ_zQ-gk
https://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US
http://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US
http://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US
http://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US
http://youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US
https://www.youtube.com/embed/DFYRQ_zQ-gk?autoplay=1
https://www.youtube.com/embed/DFYRQ_zQ-gk
http://www.youtube.com/embed/DFYRQ_zQ-gk
http://www.youtube.com/embed/DFYRQ_zQ-gk
http://www.youtube.com/embed/DFYRQ_zQ-gk
https://youtube.com/embed/DFYRQ_zQ-gk
http://youtube.com/embed/DFYRQ_zQ-gk
http://youtube.com/embed/DFYRQ_zQ-gk
https://youtube.com/embed/DFYRQ_zQ-gk
https://youtu.be/DFYRQ_zQ-gk?t=120
https://youtu.be/DFYRQ_zQ-gk
http://youtu.be/DFYRQ_zQ-gk
http://youtu.be/DFYRQ_zQ-gk
http://youtu.be/DFYRQ_zQ-gk
https://www.youtube.com/HamdiKickProduction?v=DFYRQ_zQ-gk
https://www.youtube.com/watch?v=wz4MLJBdSpw&t=67s
http://www.youtube.com/watch?v=dQw4w9WgXcQ&a=GxdCwVVULXctT2lYDEPllDR0LRTutYfW
http://www.youtube.com/embed/dQw4w9WgXcQ
http://www.youtube.com/watch?v=dQw4w9WgXcQ
https://www.youtube.com/watch?v=EL-UCUAt8DQ
http://www.youtube.com/watch?v=dQw4w9WgXcQ
http://youtu.be/dQw4w9WgXcQ
http://www.youtube.com/v/dQw4w9WgXcQ
http://www.youtube.com/e/dQw4w9WgXcQ
http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
答案 6 :(得分:0)
这与您问题中发布的链接非常相似。
String url = "https://www.youtube.com/watch?v=wZZ7oFKsKzY";
if(url.indexOf("v=") == -1) //there's no video id
return "";
String id = url.split("v=")[1]; //everything before the first 'v=' will be the first element, i.e. [0]
int end_of_id = id_to_end.indexOf("&"); //if there are other parameters in the url, get only the id's value
if(end_index != -1)
id = url.substring(0, end_of_id);
return id;
答案 7 :(得分:0)
这对我来说很简单..
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;
}
答案 8 :(得分:0)
我知道这个话题很老,但是我得到了更多的案例来获取Youtube ID,这是我的正则表达式
http(?:s)?:\/\/(?:m.)?(?:www\.)?youtu(?:\.be\/|be\.com\/(?:watch\?(?:feature=youtu.be\&)?v=|v\/|embed\/|user\/(?:[\w#]+\/)+))([^&#?\n]+)
这适用于
http://www.youtube.com/watch?v=0zM4nApSvMg&feature=feedrec_grec_index
http://www.youtube.com/user/SomeUser#p/a/u/1/QDK8U-VIH_o
http://www.youtube.com/v/0zM4nApSvMg?fs=1&hl=en_US&rel=0
http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s
http://www.youtube.com/embed/0zM4nApSvMg?rel=0
http://www.youtube.com/watch?v=0zM4nApSvMg
http://youtu.be/0zM4nApSvMg
https://www.youtube.com/watch?v=nBuae6ilH24
https://www.youtube.com/watch?v=pJegNopBLL8
http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s
https://www.youtube.com/watch?v=0zM4nApSvMg&feature=youtu.be
https://www.youtube.com/watch?v=_5BTo2oZ0SE
https://m.youtube.com/watch?feature=youtu.be&v=_5BTo2oZ0SE
https://m.youtube.com/watch?v=_5BTo2oZ0SE
https://www.youtube.com/watch?feature=youtu.be&v=_5BTo2oZ0SE&app=desktop
https://www.youtube.com/watch?v=nBuae6ilH24
https://www.youtube.com/watch?v=eLlxrBmD3H4
注意:使用正则表达式匹配组(1)获取ID
答案 9 :(得分:0)
只需在以下功能中传递youtube链接,它就会检测所有类型的youtube网址
public static String getYoutubeID(String youtubeUrl) {
if (TextUtils.isEmpty(youtubeUrl)) {
return "";
}
String video_id = "";
String expression = "^.*((youtu.be" + "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; // var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
CharSequence input = youtubeUrl;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
String groupIndex1 = matcher.group(7);
if (groupIndex1 != null && groupIndex1.length() == 11)
video_id = groupIndex1;
}
if (TextUtils.isEmpty(video_id)) {
if (youtubeUrl.contains("youtu.be/") ) {
String spl = youtubeUrl.split("youtu.be/")[1];
if (spl.contains("\\?")) {
video_id = spl.split("\\?")[0];
}else {
video_id =spl;
}
}
}
return video_id;
}
答案 10 :(得分:0)
建议结合使用两种方法以涵盖尽可能多的格式。
String ID1 = getYoutubeID1(url); regex 1
String ID2 = getYoutubeID2(url); regex 2
String ID3 = getYoutubeID3(url); regex 3
然后,使用if / switch语句选择一个不为null且有效的值。
答案 11 :(得分:0)
我认真尝试了上述所有方法,但有时能奏效,有时会失败 我发现这有帮助的任何方式-在此site
private final static String expression = "(?<=watch\\?v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";
public static String getVideoId(String videoUrl) {
if (videoUrl == null || videoUrl.trim().length() <= 0){
return null;
}
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(videoUrl);
try {
if (matcher.find())
return matcher.group();
} catch (ArrayIndexOutOfBoundsException ex) {
ex.printStackTrace();
}
return null;
}
他说
这将适用于两种直接或共享网址的youtube视频网址。
答案 12 :(得分:0)
没有正则表达式:
字符串url-“ https://www.youtube.com/watch?v=BYrumLBuzHk”
字符串video_id = url.replace(“ https://www.youtube.com/watch?v=”,“”)
所以现在您在字符串video_id中有一个视频ID。