从链接获取youtube id

我从www.youtube.com/watch?v=xxxxxxx这样的链接中获取了此代码以获取youtube ID

URL youtubeURL = new URL(link); youtubeURL.getQuery(); 

基本上这将使我很容易得到v = xxxxxxxx

但我注意到有时youtube链接会是这样的

 http://gdata.youtube.com/feeds/api/videos/xxxxxx 

我从一个feed获取链接,所以我需要为它构建一个正则表达式,还是一个解析器来为我得到它?

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

 String pattern = "(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"; Pattern compiledPattern = Pattern.compile(pattern); Matcher matcher = compiledPattern.matcher(url); if(matcher.find()){ return matcher.group(); } 

这适用于:(您还可以实施安全检查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

 public static String getYoutubeVideoId(String youtubeUrl) { String video_id=""; if (youtubeUrl != null && youtubeUrl.trim().length() > 0 && youtubeUrl.startsWith("http")) { 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; } } return video_id; } 

这个正则表达式可以解决这个问题:

 (?<=videos\/|v=)([\w-]+) 

这意味着我们首先要查找video/v=然后捕获所有以下可以在单词(字母,数字和下划线)和连字符中的字符。

java中的示例:

 public static void main(String[] args) { String link = "http://gdata.youtube.com/feeds/api/videos/xTmi7zzUa-M&whatever"; String pattern = "(?:videos\\/|v=)([\\w-]+)"; Pattern compiledPattern = Pattern.compile(pattern); Matcher matcher = compiledPattern.matcher(link); if(matcher.find()){ System.out.println(matcher.group()); } } 

输出:

 xTmi7zzUa-M 

这种模式对我有用:

 "http(?:s?)://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)([\w\-]+)(&(amp;)?[\w\?=‌​]*)?" 

source: youtube链接的正则表达式

通过此链接获得更好的解决方案。

使用以下方法从链接获取videoId。

YoutubeHelper.java

 import com.google.inject.Singleton; import java.util.regex.Matcher; import java.util.regex.Pattern; @Singleton public class YouTubeHelper { final String youTubeUrlRegEx = "^(https?)?(://)?(www.)?(m.)?((youtube.com)|(youtu.be))/"; final String[] videoIdRegex = { "\\?vi?=([^&]*)","watch\\?.*v=([^&]*)", "(?:embed|vi?)/([^/?]*)", "^([A-Za-z0-9\\-]*)"}; public String extractVideoIdFromUrl(String url) { String youTubeLinkWithoutProtocolAndDomain = youTubeLinkWithoutProtocolAndDomain(url); for(String regex : videoIdRegex) { Pattern compiledPattern = Pattern.compile(regex); Matcher matcher = compiledPattern.matcher(youTubeLinkWithoutProtocolAndDomain); if(matcher.find()){ return matcher.group(1); } } return null; } private String youTubeLinkWithoutProtocolAndDomain(String url) { Pattern compiledPattern = Pattern.compile(youTubeUrlRegEx); Matcher matcher = compiledPattern.matcher(url); if(matcher.find()){ return url.replace(matcher.group(), ""); } return url; } } 

希望这可以帮助。

如果不知道所有可能的YouTubeurl的完整规范,这似乎适用于您提供的示例:

 //*EDIT* - fixed to hopefully support more recent youtube link styles/formats: (?<=watch\?v=|/videos/|/embed/|youtu.be/)[^&#?]* 

...匹配来自以下任一URL的PjDw3azfZWI

 http://www.youtube.com/watch?v=PjDw3azfZWI#t=31m08s http://gdata.youtube.com/feeds/api/videos/PjDw3azfZWI 

如果您不知道这些来自youtube,您需要更多信息来获取特定信息,尽管这是一个非常快速的检查

请记住,如果您只尝试使用getQuery()方法的结果,则无法从http://gdata.youtube.com/feeds/api/videos/PjDw3azfZWI URL中提取结果,因为这个URL没有查询部分...

Java示例:

 Pattern rex = Pattern.compile("(?<=watch\\?v=|/videos/)[^&#]*"); Matcher m = rex.matcher(link); String YouTubeVideoID = m.group(); 

这对我有用

 public static String getYoutubeVideoId(String youtubeUrl) { String videoId = ""; if (youtubeUrl != null && youtubeUrl.trim().length() > 0 && youtubeUrl.startsWith("http")) { String expression = "^.*((youtu.be"+ "/)" + "|(v/)|(/u/w/)|(embed/)|(watch\\?))\\??v?=?([^#&\\?]*).*"; // var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(youtubeUrl); if (matcher.matches()) { String groupIndex1 = matcher.group(7); if(groupIndex1!=null && groupIndex1.length()==11) videoId = groupIndex1; } } return videoId; } 

来源链接

这不使用正则表达式但仍应该完成这项工作。

 /** * Returns the video id of a YouTube watch link. */ public static String getVideoId(String watchLink) { return watchLink.substring(watchLink.length() - 11); } 
 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; }