using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=([a-zA-Z0-9_]+)|youtu\.be\/([a-zA-Z\d_]+))(?:&.*)?$";
string input = @"http://www.youtube.com/watch?v=p72I7gRXpg
http://youtube.com/watch?v=bQVoAWSP7k4
http://www.youtube.com/watch?v=bQVoAWSP7k4
https://youtu.be/6WZoMAVCggM
http://www.youtube.com/watch?v=6WZoMAVCggM&
http://www.youtube.com/watch?v=bQVoAWSP7k4&feature=popular
http://www.youtube.com/watch?v=McNqjYiFmyQ&feature=related&bhablah
www.youtube.com/watch?v=McNqjYiFmyQ&feature=related&bhablah
youtu.be/6WZoMAVCggM
youtube.com/watch?v=bQVoAWSP7k4
http://youtube.com/watch?v=bQVoAWSP7k4
http://youtu.be/6WZoMAVCg";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx