ASP/C# regex for youtube video

asp.net-mvc, c#, regex

Solution

RegEx for validating a youtube URL and extracting the Video_ID:

^(?:https?\:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v\=))([\w-]{10,12})(?:$|\&|\?\#).*

Escaped RegEx:

^(?:https?\\:\\/\\/)?(?:www\\.)?(?:youtu\\.be\\/|youtube\\.com\\/(?:embed\\/|v\\/|watch\\?v\\=))([\\w-]{10,12})(?:$|\\&|\\?\\#).*

Test here: http://fiddle.re/w1nn6

Problem

I'm having problems adjusting my Regex originally used with Javascript to use with ASP.NET Regular Expression Validator Control. I'm currently using: ``` (?:https?://)?(?:www\\.)?(?:youtu\\.be/|youtube\\.com(?:/embed/|/v/|.*v=))([\\w-]{10,12})($|&).* ``` but it doesn't work. (Always returns validation error) I also wanted to know if I can use regex to extract youtube ID from the video URL, and how? Thank you for your time.

Original source