tym32167的回答拋出在var uri = new Uri(url);
異常時url
沒有一個計劃,像「www.youtu.be/AAAAAAAAA08 」。
此外,錯誤videoId
是返回一些網址。
所以這裏是我的代碼基於tym32167的。
static private string GetYouTubeVideoIdFromUrl(string url)
{
Uri uri = null;
if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
{
try
{
uri = new UriBuilder("http", url).Uri;
}
catch
{
// invalid url
return "";
}
}
string host = uri.Host;
string[] youTubeHosts = { "www.youtube.com", "youtube.com", "youtu.be", "www.youtu.be" };
if (!youTubeHosts.Contains(host))
return "";
var query = HttpUtility.ParseQueryString(uri.Query);
if (query.AllKeys.Contains("v"))
{
return Regex.Match(query["v"], @"^[a-zA-Z0-9_-]{11}$").Value;
}
else if (query.AllKeys.Contains("u"))
{
// some urls have something like "u=/watch?v=AAAAAAAAA16"
return Regex.Match(query["u"], @"/watch\?v=([a-zA-Z0-9_-]{11})").Groups[1].Value;
}
else
{
// remove a trailing forward space
var last = uri.Segments.Last().Replace("/", "");
if (Regex.IsMatch(last, @"^v=[a-zA-Z0-9_-]{11}$"))
return last.Replace("v=", "");
string[] segments = uri.Segments;
if (segments.Length > 2 && segments[segments.Length - 2] != "v/" && segments[segments.Length - 2] != "watch/")
return "";
return Regex.Match(last, @"^[a-zA-Z0-9_-]{11}$").Value;
}
}
讓我們來測試它。
string[] urls = {"http://youtu.be/AAAAAAAAA01",
"http://www.youtube.com/embed/watch?feature=player_embedded&v=AAAAAAAAA02",
"http://www.youtube.com/embed/watch?v=AAAAAAAAA03",
"http://www.youtube.com/embed/v=AAAAAAAAA04",
"http://www.youtube.com/watch?feature=player_embedded&v=AAAAAAAAA05",
"http://www.youtube.com/watch?v=AAAAAAAAA06",
"http://www.youtube.com/v/AAAAAAAAA07",
"www.youtu.be/AAAAAAAAA08",
"youtu.be/AAAAAAAAA09",
"http://www.youtube.com/watch?v=i-AAAAAAA14&feature=related",
"http://www.youtube.com/attribution_link?u=/watch?v=AAAAAAAAA15&feature=share&a=9QlmP1yvjcllp0h3l0NwuA",
"http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&u=/watch?v=AAAAAAAAA16&feature=em-uploademail",
"http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&feature=em-uploademail&u=/watch?v=AAAAAAAAA17",
"http://www.youtube.com/v/A-AAAAAAA18?fs=1&rel=0",
"http://www.youtube.com/watch/AAAAAAAAA11",};
Console.WriteLine("***Youtube urls***");
foreach (string url in urls)
{
Console.WriteLine("{0}\n-> {1}", url, GetYouTubeVideoIdFromUrl(url));
}
string[] invalidUrls = {
"ww.youtube.com/v/AAAAAAAAA13",
"http:/www.youtube.com/v/AAAAAAAAA13",
"http://www.youtub1e.com/v/AAAAAAAAA13",
"http://www.vimeo.com/v/AAAAAAAAA13",
"www.youtube.com/b/AAAAAAAAA13",
"www.youtube.com/v/AAAAAAAAA1",
"www.youtube.com/v/AAAAAAAAA1&",
"www.youtube.com/v/AAAAAAAAA1/",
".youtube.com/v/AAAAAAAAA13"};
Console.WriteLine("***Invalid youtube urls***");
foreach (string url in invalidUrls)
{
Console.WriteLine("{0}\n-> {1}", url, GetYouTubeVideoIdFromUrl(url));
}
結果(一切是正常的)
***Youtube urls***
http://youtu.be/AAAAAAAAA01
-> AAAAAAAAA01
http://www.youtube.com/embed/watch?feature=player_embedded&v=AAAAAAAAA02
-> AAAAAAAAA02
http://www.youtube.com/embed/watch?v=AAAAAAAAA03
-> AAAAAAAAA03
http://www.youtube.com/embed/v=AAAAAAAAA04
-> AAAAAAAAA04
http://www.youtube.com/watch?feature=player_embedded&v=AAAAAAAAA05
-> AAAAAAAAA05
http://www.youtube.com/watch?v=AAAAAAAAA06
-> AAAAAAAAA06
http://www.youtube.com/v/AAAAAAAAA07
-> AAAAAAAAA07
www.youtu.be/AAAAAAAAA08
-> AAAAAAAAA08
youtu.be/AAAAAAAAA09
-> AAAAAAAAA09
http://www.youtube.com/watch?v=i-AAAAAAA14&feature=related
-> i-AAAAAAA14
http://www.youtube.com/attribution_link?u=/watch?v=AAAAAAAAA15&feature=share&a=9QlmP1yvjcllp0h3l0NwuA
-> AAAAAAAAA15
http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&u=/watch?v=AAAAAAAAA16&feature=em-uploademail
-> AAAAAAAAA16
http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&feature=em-uploademail&u=/watch?v=AAAAAAAAA17
-> AAAAAAAAA17
http://www.youtube.com/v/A-AAAAAAA18?fs=1&rel=0
-> A-AAAAAAA18
http://www.youtube.com/watch/AAAAAAAAA11
-> AAAAAAAAA11
***Invalid youtube urls***
ww.youtube.com/v/AAAAAAAAA13
->
http:/www.youtube.com/v/AAAAAAAAA13
->
http://www.youtub1e.com/v/AAAAAAAAA13
->
http://www.vimeo.com/v/AAAAAAAAA13
->
www.youtube.com/b/AAAAAAAAA13
->
www.youtube.com/v/AAAAAAAAA1
->
www.youtube.com/v/AAAAAAAAA1&
->
www.youtube.com/v/AAAAAAAAA1/
->
.youtube.com/v/AAAAAAAAA13
->
檢查此[正則表達式(http://stackoverflow.com/a/27796139/6290553) –