【问题标题】:getting the youtube id from a link从链接获取 youtube id
【发布时间】:2011-12-05 12:44:41
【问题描述】:

我得到了这个代码来从链接中获取 youtube id,比如 www.youtube.com/watch?v=xxxxxxx

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

基本上这会让我很容易得到 id v=xxxxxxxx

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

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

我正在从提要中获取链接 那么我需要为此构建一个正则表达式还是有一个解析器来为我获取它?

【问题讨论】:

标签: java regex parsing url youtube


【解决方案1】:

尝试了其他的,但在我的情况下失败了 - 调整正则表达式以适合我的网址

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

【讨论】:

  • 非常感谢你!!
  • 它不漂亮,但给你:(?&lt;=watch\?v=|/videos/|embed\/|youtu.be\/|\/v\/|watch\?v%3D|%2Fvideos%2F|embed%2F|youtu.be%2F|%2Fv%2F)[^#\&amp;\?\n]*
  • @Peril 这应该是答案,请标记它。
【解决方案2】:
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;
 }

【讨论】:

    【解决方案3】:

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

    (?<=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
    

    【讨论】:

    • 这不会只给我 id
    • 使用http://gdata.youtube.com/feeds/api/videos/xxxxxx 作为数据我得到xTmi7zzUa-M 作为输出。我是否误读了您的问题,而您要求的正则表达式可以让您同时解析 v=xxxxxxxx 和另一个?
    • 其实。该正则表达式适用于这两种情况。你得到什么结果?
    • 但是你写的正则表达式会让我得到 id 和其他东西在这里检查gskinner.com/RegExr
    • 最后的“$”使它只能捕获.../videos/ 之外的所有内容。如果链接上有任何其他参数(即 v=xxxxxxx&other_param=yyyyy...),则在第一个示例中将不起作用
    【解决方案4】:

    这种模式对我有用:

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

    来源:Regular expression for youtube links

    【讨论】:

      【解决方案5】:

      从此link 得到了更好的解决方案。

      使用以下方法从链接中获取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;
          } 
      } 
      

      希望这会有所帮助。

      【讨论】:

        【解决方案6】:

        在不知道所有可能的 YouTube 网址的完整规范的情况下,这似乎适用于您提供的示例:

        //*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();
        

        【讨论】:

        • @krishan 确实不是(据我所知,当这个问题被问到时,这甚至不是一个有效的 URL...)但是如果你有兴趣,这应该适用于 youtu.be以及/embed/ 链接:(?&lt;=watch\?v=|/videos/|/embed/|youtu.be/)[^&amp;#?]*(我会更新我的答案)-如果不是,请告诉我,因为我刚才是手工制作的...
        • 我的问题得到了解决方案stackoverflow.com/questions/25718304/…
        【解决方案7】:

        这对我有用

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

        来源link

        【讨论】:

          【解决方案8】:

          这不使用正则表达式,但仍然可以完成这项工作。

          /**
           * Returns the video id of a YouTube watch link.
           */
          public static String getVideoId(String watchLink)
          {
              return watchLink.substring(watchLink.length() - 11);
          }
          

          【讨论】:

            【解决方案9】:
            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;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-07-18
              • 1970-01-01
              • 2011-11-27
              • 2023-03-28
              • 1970-01-01
              相关资源
              最近更新 更多