Extract the video ID from youtube url in .net

It is not required to use regular expressions here

var url = @"https://www.youtube.com/watch?v=6QlW4m9xVZY";
var uri = new Uri(url);

// you can check host here => uri.Host <= "www.youtube.com"

var query = HttpUtility.ParseQueryString(uri.Query);
var videoId = query["v"];

// videoId = 6QlW4m9xVZY

Ok, example above is working, when you have v=videoId as parameter. If you have videoId as segment, you can use this:

var url = "http://youtu.be/AAAAAAAAA09";
var uri = new Uri(url);

var videoid = uri.Segments.Last(); // AAAAAAAAA09

Combining all together, we can get

var url = @"https://www.youtube.com/watch?v=Lvcyj1GfpGY&list=PLolZLFndMkSIYef2O64OLgT-njaPYDXqy";
var uri = new Uri(url);

// you can check host here => uri.Host <= "www.youtube.com"

var query = HttpUtility.ParseQueryString(uri.Query);

var videoId = string.Empty;

if (query.AllKeys.Contains("v"))
{
    videoId = query["v"];
}
else
{
    videoId = uri.Segments.Last();
}

Of course, I don't know anything about your requirements, but, I hope it helps.


The problem is that the regex cannot check for a string that is required before the mining action and at the same time use this sting as the mining action itself.

For example let's check "http://www.youtu.be/v/AAAAAAAAA07" YouTu.be is mandatory at the beginning of the URL but the mining action is "/v/(11 chars)"

At "http://www.youtu.be/AAAAAAAAA07" the mining action is "youtu.be/(11 chars)"

This cannot be at the same regex and this is why we cannot check for domain and extract the id at the same regex.

I decided to check the domain authority from a list of valid domains and then extract the id from the URL.

 private const string YoutubeLinkRegex = "(?:.+?)?(?:\\/v\\/|watch\\/|\\?v=|\\&v=|youtu\\.be\\/|\\/v=|^youtu\\.be\\/)([a-zA-Z0-9_-]{11})+";
 private static Regex regexExtractId = new Regex(YoutubeLinkRegex, RegexOptions.Compiled);
 private static string[] validAuthorities = { "youtube.com", "www.youtube.com", "youtu.be", "www.youtu.be" };

 public string ExtractVideoIdFromUri(Uri uri)
 {
     try
     {
        string authority = new UriBuilder(uri).Uri.Authority.ToLower();

        //check if the url is a youtube url
        if (validAuthorities.Contains(authority))
        {
            //and extract the id
            var regRes = regexExtractId.Match(uri.ToString());
            if (regRes.Success)
            {
                return regRes.Groups[1].Value;
            }
        }
     }catch{}


     return null;
 }

UriBuilder is preferred because it can understand a wider range of URLs than Uri class. It can create Uri from URLs that doesn't contain scheme such as "youtube.com".

The function is returning null(correctly) with the following test 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"

Tags:

C#

.Net

Regex