using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^rgba?\((?:(?:(?:[0-9]{1,3} ){2}[0-9]{1,3}|(?:[0-9]{1,3}% ){2}[0-9]{1,3}%)(?: \/ (?:[0-9]{1,3}%|1|0|0?\.[0-9]+))?|(?:(?:[0-9]{1,3}, ){2}[0-9]{1,3}|(?:[0-9]{1,3}%, ){2}[0-9]{1,3}%)(?:, (?:[0-9]{1,3}%|1|0|0?\.[0-9]+))?)\)$";
string input = @"// invalid
rgb()
rgba()
rgb(123)
rgb(11 22 )
rgb(11 22 2 / 03)
rgb(11 22 2 / 10)
rgb(1 2 3 / 0.34%)
rgb(11% 22 33)
rgb(111 222% 333)
rgb(1 22 333 )
rgb(1 2 3, 1)
rgb(1 2, 3 / 1)
// valid
rgb(111 22 333)
rgb(1 2 3)
rgb(11 22 33)
rgb(111 222 333)
rgb(1 22 333)
rgb(1 2 3 / 1)
rgb(11 22 2 / 0)
rgb(1 2 3 / 0.34)
rgb(1 2 3 / .34)
rgb(1 2 3 / 0.34)
rgb(1 2 3 / 1%)
rgb(1 2 3 / 10%)
rgb(1 2 3 / 100%)
rgb(11 22 2 / 0%)
rgb(1 2 3 / 34%)
rgb(1 2 3 / 4%)
rgb(111 22 333)
rgb(1% 2% 3%)
rgb(11% 22% 2% / 0)
rgb(12%, 0%, 100%)
rgb(111, 22, 333)
rgb(1, 2, 3)
rgb(1, 2, 3, 1)
";
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