如何使用Java中的regex从YouTubeurl获取videoID

从javascript答案移植到Java版本JavaScript REGEX:如何从URL获取YouTubevideoID?

认为这个问题不适用于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; } 

适用于URL(也适用于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 

以前的答案对我不起作用..这是正确的方法。 它适用于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"; } } 

对我来说,对于您发布的示例链接,此正则表达式看起来更精确(将ID捕获到组1):

 http://(?:www\.)?youtu(?:\.be/|be\.com/(?:watch\?v=|v/|embed/|user/(?:[\w#]+/)+))([^&#?\n]+) 

在演示中 ,请参阅右下方窗格中的捕获组。 但是,对于第二场比赛,不完全确定这是一个正确的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); } 

试过其他的但在我的情况下失败了 – 调整正则表达式以适应我的url

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

这个适用于以下url

 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 

希望它会帮助一些人。 谢谢

这与您的问题中发布的链接非常相​​似。

 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, ie [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; 

这对我来说很简单..

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

只需通过以下function中的youtube链接,它将检测所有类型的youtubeurl

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

我知道这个话题很老,但是我得到了更多的案例来获取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