久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C#正則表達式通過url從youtube和vimeo獲取視頻ID

C# regex to get video id from youtube and vimeo by url(C#正則表達式通過url從youtube和vimeo獲取視頻ID)
本文介紹了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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

LINQ to SQL and Concurrency Issues(LINQ to SQL 和并發問題)
SQL Server 2005 Transaction Level and Stored Procedures(SQL Server 2005 事務級和存儲過程)
Yield return from a try/catch block(try/catch 塊的收益回報)
Should I call Parameters.Clear when reusing a SqlCommand with a transation?(重用帶有事務的 SqlCommand 時,我應該調用 Parameters.Clear 嗎?)
Does SqlTransaction need to have Dispose called?(SqlTransaction 是否需要調用 Dispose?)
Reason for System.Transactions.TransactionInDoubtException(System.Transactions.TransactionInDoubtException 的原因)
主站蜘蛛池模板: 欧美日韩专区 | 狠狠色狠狠色综合日日92 | 米奇成人网 | 久久99这里只有精品 | 欧美成人精品一区二区三区 | 成人激情视频网 | 99精品国自产在线观看 | 在线免费观看色 | 精品国产欧美一区二区三区成人 | 亚洲精品麻豆 | 午夜天堂精品久久久久 | 中文字幕高清 | 精品国产乱码久久久久久蜜柚 | 综合在线视频 | 性精品| 三级视频在线观看 | 国产精品久久一区二区三区 | 99爱在线观看 | 亚洲 中文 欧美 | 国产乱码精品1区2区3区 | 成人18亚洲xxoo | 日韩色视频 | 国产一区不卡 | 欧美激情免费在线 | 免费在线播放黄色 | 福利av在线 | 日韩精品在线一区 | 91亚洲精品国偷拍自产在线观看 | h视频在线播放 | www.99热这里只有精品 | 一本色道久久综合亚洲精品高清 | 91在线电影 | 91成人精品 | 久久久久国 | 欧美精品一级 | 日韩欧美三级 | 久久久91 | 国产h在线 | 在线观看一区 | 亚洲欧美在线观看 | 婷婷综合在线 |