C#正则表达式通过url从youtube和vimeo获取视频ID [英] C# regex to get video id from youtube and vimeo by url

查看:41
本文介绍了C#正则表达式通过url从youtube和vimeo获取视频ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙于创建两个正则表达式来过滤来自 youtube 和 vimeo 视频的 ID.我已经有了以下表达式;

I'm busy trying to create two regular expressions to filter the id from youtube and vimeo video's. I've already got the following expressions;

YouTube: (youtube.com/)(.*)v=([a-zA-Z0-9-_]+)
Vimeo: vimeo.com/([0-9]+)$

正如我在下面解释的,有两种类型的 url 与我已经创建的正则表达式匹配.来自 Vimeo 和 YouTube 的其他几种类型的 url 不包含在表达式中.我最喜欢的是这一切都可以用两种表达方式来涵盖.一种用于所有 Vimeo 视频,一种用于所有 youtube 视频.我一直忙于尝试一些不同的表达方式,但到目前为止都没有成功.我仍在努力掌握正则表达式,所以我希望我走在正确的道路上,有人可以帮助我!如果需要更多信息,请告诉我!

As i explained below there are 2 types of urls matched by the regular expressions i already created. Several other types of urls from Vimeo and YouTube aren't coverd by the expressions. What i prefer most is that all this can be covered in two expressions. One for all Vimeo video's and one for all youtube video's. I've been busy experimenting with some different expressions, but no succes so far. I'm still trying to master regular expressions, so i hope i'm on the right way and somebody can help me out! If more information is required, please let me know!

VIMEO URL 不匹配:

VIMEO URLs NOT MATCHED:

http://vimeo.com/channels/hd#11384488
http://vimeo.com/groups/brooklynbands/videos/7906210
http://vimeo.com/staffpicks#13561592

YouTube 网址不匹配

YOUTUBE URLs NOT MATCHED

http://www.youtube.com/user/username#p/a/u/1/bpJQZm_hkTE
http://www.youtube.com/v/bpJQZm_hkTE
http://youtu.be/bpJQZm_hkTE

匹配的网址

http://www.youtube.com/watch?v=bWTyFIYPtYU&feature=popular
http://vimeo.com/834881

这个想法是用两个正则表达式匹配上面提到的所有 url.一种用于 vimeo,一种用于 youtube.

The idea is to match all the url's mentioned above with two regular expressions. One for vimeo and one for youtube.

回答塞迪斯后更新:

这就是我现在的表情

public static readonly Regex VimeoVideoRegex = new Regex(@"vimeo.com/(?:.*#|.*/videos/)?([0-9]+)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
public static readonly Regex YoutubeVideoRegex = new Regex(@"youtu(?:.be|be.com)/(?:(.*)v(/|=)|(.*/)?)([a-zA-Z0-9-_]+)", RegexOptions.IgnoreCase);

在代码中我有

var youtubeMatch = url.match(YoutubeVideoRegex );
var vimeoMatch = url.match(VimeoVideoRegex );

var youtubeIndex = (youtubeMatch.length - 1)
var youtubeId = youtubeMatch[youtubeIndex];

如您所见,我现在需要找到 videoId 在数组中的索引,其中包含从正则表达式返回的匹配项.但我希望它只返回 id 本身,所以当 vimeo 的 youtube 决定更改那里的 url 时,我不需要修改代码.对此有什么提示吗?

As you can see i now need to find the index where the videoId is in the array with matches returned from the regex. But i want it to only return the id itselfs, so i don't need to modify the code when youtube of vimeo ever decide to change there urls. Any tips on this?

推荐答案

我对这些示例进行了尝试并想出了这些:

I had a play around with the examples and came up with these:

Youtube: youtu(?:.be|be.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)
Vimeo: vimeo.com/(?:.*#|.*/videos/)?([0-9]+)

他们应该匹配所有给定的.(?: ...) 表示括号内的所有内容都不会被捕获.所以只需要获取id即可.

And they should match all those given. The (?: ...) means that everything inside the bracket won't be captured. So only the id should be obtained.

我自己也是一个正则表达式新手,所以如果有人进来尖叫着不听我的,请不要感到惊讶,但希望这些会有所帮助.

I'm a bit of a regex novice myself, so don't be surprised if someone else comes in here screaming not to listen to me, but hopefully these will be of help.

我发现这个网站在制定模式方面非常有用:http://www.regexpal.com/

I find this website extremely useful in working out the patterns: http://www.regexpal.com/

像这样获取id:

string url = ""; //url goes here!

Match youtubeMatch = YoutubeVideoRegex.Match(url);
Match vimeoMatch = VimeoVideoRegex.Match(url);

string id = string.Empty;

if (youtubeMatch.Success)
    id = youtubeMatch.Groups[1].Value; 

if (vimeoMatch.Success)
    id = vimeoMatch.Groups[1].Value;

这适用于普通的旧 c#.net,不能保证 asp.net

That works in plain old c#.net, can't vouch for asp.net

这篇关于C#正则表达式通过url从youtube和vimeo获取视频ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆