如何使用 Dart 解析 YouTube URLs 以从 URL 获取视频 ID?
How do I parse YouTube URLs to get the video ID from a URL using Dart?
我正在尝试将 *this 函数转换为 Dart,但到目前为止没有成功。我对正则表达式的部分有点困惑。我使用 regExp.allMatches***(url)*** 实现 returns 整个 url.
Javascript版本:
function youtube_parser(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
return (match&&match[7].length==11)? match[7] : false;
}
WIP Dart 版本:
RegExp regExp = new RegExp(
r'.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*',
caseSensitive: false,
multiLine: false,
);
final match = regExp.allMatches(url);
谁能帮我解决这个问题?
编辑:
输入网址:
final urls = [
'http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index',
'http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o',
'http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0',
'http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s',
'http://www.youtube.com/embed/0zM3nApSvMg?rel=0',
'http://www.youtube.com/watch?v=0zM3nApSvMg',
'http://youtu.be/0zM3nApSvMg',
];
每个 getYoutubeVideoId(url) 的输出应该是 0zM3nApSvMg
提前致谢,
费利佩
好吧,我没有完整的解决方案,但我希望这会对你有所帮助,你的问题是你正在使用一个 RegExp,它 return 将结果作为一个组,你需要 select:
final urls = [
'http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index',
'http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o',
'http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0',
'http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s',
'http://www.youtube.com/embed/0zM3nApSvMg?rel=0',
'http://www.youtube.com/watch?v=0zM3nApSvMg',
'http://youtu.be/0zM3nApSvMg',
];
void main() {
RegExp regExp = new RegExp(
r'.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*',
caseSensitive: false,
multiLine: false,
);
for (final url in urls) {
final match = regExp.firstMatch(url).group(1); // <- This is the fix
print('$url -> $match');
}
}
此代码将 return:
http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index -> 0zM3nApSvMg
http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o -> QdK8U-VIH_o
http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 -> 0zM3nApSvMg
http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s -> 0zM3nApSvMg
http://www.youtube.com/embed/0zM3nApSvMg?rel=0 -> 0zM3nApSvMg
http://www.youtube.com/watch?v=0zM3nApSvMg -> 0zM3nApSvMg
http://youtu.be/0zM3nApSvMg -> 0zM3nApSvMg
如您所见,某些 URL 仍然存在一些问题,但我希望我的解决方案能帮助您使 RegExp 正常工作。
我正在尝试将 *this 函数转换为 Dart,但到目前为止没有成功。我对正则表达式的部分有点困惑。我使用 regExp.allMatches***(url)*** 实现 returns 整个 url.
Javascript版本:
function youtube_parser(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
return (match&&match[7].length==11)? match[7] : false;
}
WIP Dart 版本:
RegExp regExp = new RegExp(
r'.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*',
caseSensitive: false,
multiLine: false,
);
final match = regExp.allMatches(url);
谁能帮我解决这个问题?
编辑:
输入网址:
final urls = [
'http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index',
'http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o',
'http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0',
'http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s',
'http://www.youtube.com/embed/0zM3nApSvMg?rel=0',
'http://www.youtube.com/watch?v=0zM3nApSvMg',
'http://youtu.be/0zM3nApSvMg',
];
每个 getYoutubeVideoId(url) 的输出应该是 0zM3nApSvMg
提前致谢,
费利佩
好吧,我没有完整的解决方案,但我希望这会对你有所帮助,你的问题是你正在使用一个 RegExp,它 return 将结果作为一个组,你需要 select:
final urls = [
'http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index',
'http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o',
'http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0',
'http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s',
'http://www.youtube.com/embed/0zM3nApSvMg?rel=0',
'http://www.youtube.com/watch?v=0zM3nApSvMg',
'http://youtu.be/0zM3nApSvMg',
];
void main() {
RegExp regExp = new RegExp(
r'.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*',
caseSensitive: false,
multiLine: false,
);
for (final url in urls) {
final match = regExp.firstMatch(url).group(1); // <- This is the fix
print('$url -> $match');
}
}
此代码将 return:
http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index -> 0zM3nApSvMg
http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o -> QdK8U-VIH_o
http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 -> 0zM3nApSvMg
http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s -> 0zM3nApSvMg
http://www.youtube.com/embed/0zM3nApSvMg?rel=0 -> 0zM3nApSvMg
http://www.youtube.com/watch?v=0zM3nApSvMg -> 0zM3nApSvMg
http://youtu.be/0zM3nApSvMg -> 0zM3nApSvMg
如您所见,某些 URL 仍然存在一些问题,但我希望我的解决方案能帮助您使 RegExp 正常工作。