C#: Get YouTube Video ID From URL With Regex

I’m just posting this here for those of you who want a quick way to grab that YouTube video key from a URL. First off, I snagged the following regex from this StackOverflow post. It looks like this:

I gave it a test drive in RegexHero and it seemed to work quite well. There are more complicated solutions out there but this seems to do the job. The video key shows up in the second regex group (or group number one). To get the key we run a Regex.Match and grab the value of that second group:

Now you can store that in the database and recreate embed code on the fly.

Creating your first programming language is easier than you think.

Understanding how a programming language works is a surefire way to be a better developer. Read more.

Suggested Reading

written by Tim Taylor

3 Responses to “C#: Get YouTube Video ID From URL With Regex”

  1. Chirag says:

    Hi this dosn’t work for me. for following YouTube Url.

    Any idea y..??

  2. Mark says:

    Failed for me ‘sometimes’ until I replaced & with &
    Using C#

Leave a Reply