且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#正则表达式通过url从***和vimeo获取视频ID

更新时间:2022-01-07 21:42:54

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

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

***: 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 ***Match = ***VideoRegex.Match(url);
Match vimeoMatch = VimeoVideoRegex.Match(url);

string id = string.Empty;

if (***Match.Success)
    id = ***Match.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