Extract the video ID from youtube url in .net
<p>I am struggling with a regex to extract the video ID from a youtube url.</p>

<p><code>"(?:.+?)?(?:\\/v\\/|watch\\/|\\?v=|\\&amp;v=|youtu\\.be\\/|\\/v=|^youtu\\.be\\/)([a-zA-Z0-9_-]{11})+";</code></p>

<p>It's working since it matches the video ID but I want to restrict it at the youtube domain, i don't want it to match the id if the domain differs from youtube.com or youtu.be. Unfortunately I cannot understand this regex to apply the restriction.</p>

<p>I want to match the id only when the domain is :</p>

<ul>
<li>www.youtube.com</li>
<li>youtube.com</li>
<li>youtu.be</li>
<li>www.youtu.be</li>
</ul>

<p>with http or https at the front (or without)</p>

<p>The above mentioned regex is successfully matching the youtube id of the following examples:</p>

<pre><code>"http://youtu.be/AAAAAAAAA01"
"http://www.youtube.com/embed/watch?feature=player_embedded&amp;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&amp;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&amp;feature=related"
"http://www.youtube.com/attribution_link?u=/watch?v=AAAAAAAAA15&amp;feature=share&amp;a=9QlmP1yvjcllp0h3l0NwuA"
"http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&amp;u=/watch?v=AAAAAAAAA16&amp;feature=em-uploademail"
"http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&amp;feature=em-uploademail&amp;u=/watch?v=AAAAAAAAA17"
"http://www.youtube.com/v/A-AAAAAAA18?fs=1&amp;rel=0"
"http://www.youtube.com/watch/AAAAAAAAA11"
</code></pre>

<p>The current code that checks the url right now is:</p>

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


    public string ExtractVideoIdFromUrl(string url)
    {
        //extract the id
        var regRes = regexExtractId.Match(url);
        if (regRes.Success)
        {
            return regRes.Groups[1].Value;
        }
        return null;
    }
</code></pre>