【问题标题】:Flutter Dart : How to extract youtube video ID from video URL?Flutter Dart:如何从视频 URL 中提取 youtube 视频 ID?
【发布时间】:2019-10-24 02:34:52
【问题描述】:

如何使用 dart 正则表达式从视频 URL 中提取 YouTube 视频 ID?

示例网址

https://www.youtube.com/watch?v=SEkUienM2oY&t=1265s

或

https://m.youtube.com/watch?v=SEkUienM2oY&t=1265s

返回

SEkUienM2oY&t=1265s

它对我有用

  String getVideoID(String url) {
    url = url.replaceAll("https://www.youtube.com/watch?v=", "");
    url = url.replaceAll("https://m.youtube.com/watch?v=", "");
    return url;
  }

但是如何用正则表达式做到这一点?

【问题讨论】:

    标签: regex flutter dart


    【解决方案1】:

    这样的正则表达式也可以在没有时间的情况下解析 Youtube 视频网址

    .*\?v=(.+?)($|[\&])
    

    dart 中的完整示例:

      String? _getYoutubeVideoIdByURL (String url) {
        final regex = RegExp(r'.*\?v=(.+?)($|[\&])', caseSensitive: false);
    
        try {
          if (regex.hasMatch(url)) {
            return regex.firstMatch(url)!.group(1);
          }
        } catch (e) {
          return null;
        }
      }
    

    【讨论】:

    • 这里的“文本”是什么?
    【解决方案2】:
    static String? convertUrlToId(String url, {bool trimWhitespaces = true}) {
        if (!url.contains("http") && (url.length == 11)) return url;
        if (trimWhitespaces) url = url.trim();
    
        for (var exp in [
          RegExp(
              r"^https:\/\/(?:www\.|m\.)?youtube\.com\/watch\?v=([_\-a-zA-Z0-9]{11}).*$"),
          RegExp(
              r"^https:\/\/(?:www\.|m\.)?youtube(?:-nocookie)?\.com\/embed\/([_\-a-zA-Z0-9]{11}).*$"),
          RegExp(r"^https:\/\/youtu\.be\/([_\-a-zA-Z0-9]{11}).*$")
        ]) {
          Match? match = exp.firstMatch(url);
          if (match != null && match.groupCount >= 1) return match.group(1);
        }
    
        return null;
    }
    

    【讨论】:

    • 请说明如何以及为何解决问题。它似乎与@shanu12joshi 的解决方案非常相似。最好描述一下它的不同之处。
    【解决方案3】:

    没有正则表达式,我希望这能完美运行 将此依赖项添加到您的 pubspec.yaml 文件中

    youtube_player_flutter: ^6.1.0+4

    try {
    
        videoIdd = YoutubePlayer.convertUrlToId("your url here");
        print('this is '+videoId);
    
      } on Exception catch (exception) {
    
        // only executed if error is of type Exception
        print('exception');
    
      } catch (error) {
    
        // executed for errors of all types other than Exception
         print('catch error');
       //  videoIdd="error";
    
    }
    

    【讨论】:

      【解决方案4】:

      pub上有包获取idyoutube_parser

      youtube_parser 从各种 YouTube URL 中提取 ID 的极简库

      import 'package:youtube_parser/youtube_parser.dart';
      
      String foo = getIdFromUrl('https://www.youtube.com/watch?v=CBWlfU_na-2');
      // returns 'CBWlfU_na-2'
      
      String bar = getIdFromUrl('https://www.notyoutube.com/watch?v=CBWlfU_na-2');
      // returns null
      

      这也是 Uri.parse 现在最终使用的东西

      【讨论】:

        【解决方案5】:

        如果我们所有的 URL 都与问题中的输入字符串相似,我们可以简单地使用类似于以下的表达式提取这些 ID:

        .*\?v=(.+?)&.+
        

        我们想要的输出在这个捕获组中:(.+?)

        Demo

        参考

        看来我们应该有 11 alphanumeric chars [A-Za-z0-9]{11} 来表示该 ID。如果您想设计复杂的表达式,这可能是独一无二的:

        【讨论】:

        【解决方案6】:
        static String convertUrlToId(String url, {bool trimWhitespaces = true}) {
          assert(url?.isNotEmpty ?? false, 'Url cannot be empty');
          if (!url.contains("http") && (url.length == 11)) return url;
          if (trimWhitespaces) url = url.trim();
        
          for (var exp in [
            RegExp(
                r"^https:\/\/(?:www\.|m\.)?youtube\.com\/watch\?v=([_\-a-zA-Z0-9]{11}).*$"),
            RegExp(
                r"^https:\/\/(?:www\.|m\.)?youtube(?:-nocookie)?\.com\/embed\/([_\-a-zA-Z0-9]{11}).*$"),
            RegExp(r"^https:\/\/youtu\.be\/([_\-a-zA-Z0-9]{11}).*$")
          ]) {
            Match match = exp.firstMatch(url);
            if (match != null && match.groupCount >= 1) return match.group(1);
          }
        
          return null;
        }
        

        【讨论】:

          猜你喜欢
          • 2019-12-07
          • 1970-01-01
          • 2012-05-22
          • 2011-03-28
          • 2017-02-08
          • 1970-01-01
          • 1970-01-01
          • 2016-09-28
          相关资源
          最近更新 更多