問題描述
我正忙于創建兩個正則表達式來過濾來自 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!