C#正则表达式通过链接直接获得来自YouTube的视频ID和Vimeo [英] C# regex to get video id from youtube and vimeo by url

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

问题描述

我忙于创建两个常规EX pressions以从YouTube和Vimeo视频的过滤器的ID。我已经得到了以下EX pressions;

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匹配的常规EX pressions我已经创建。其他几种类型的Vimeo的和YouTube网址不被前pressions coverd。我最preFER的是,这一切都可以包含在两位前pressions。我为人人Vimeo的视频的,一个用于所有的YouTube视频的。我一直忙于一些不同的EX pressions试验,但没有更迭至今。我还在努力掌握常规的前pressions,所以我希望我在正确的道路,有人可以帮我!如果需要更多的信息,请让我知道!

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网址不匹配关键词:

VIMEO URLs NOT MATCHED:

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

YOUTUBE网址NOT MATCHED

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的上面有两个常规的前pressions提及。一个用于VIMEO,一个是YouTube上。

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

答Sedith AFTER UPDATE:

UPDATE AFTER ANSWER Sedith:

这是我的前pressions现在怎么看

This is how my expressions look now

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

而在code,我有

And in code i have

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

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

正如你可以看到我现在需要找到索引,其中VIDEOID是用火柴数组中从正则表达式返回。但我希望它只是返回ID itselfs,所以我并不需要修改code时,对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#正则表达式通过链接直接获得来自YouTube的视频ID和Vimeo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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