using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^rgba?\((?:(25[0-5]|2[0-4]\d|1?\d{1,2}|(?:\d{1,2}|100)%),\s*(25[0-5]|2[0-4]\d|1?\d{1,2}|(?:\d{1,2}|100)%),\s*(25[0-5]|2[0-4]\d|1?\d{1,2}|(?:\d{1,2}|100)%)(?:,\s*((?:\d{1,2}|100)%|0(?:\.\d+)?|1))?|(25[0-5]|2[0-4]\d|1?\d{1,2}|(?:\d{1,2}|100)%)\s+(25[0-5]|2[0-4]\d|1?\d{1,2}|(?:\d{1,2}|100)%)\s+(25[0-5]|2[0-4]\d|1?\d{1,2}|(?:\d{1,2}|100)%)(?:\s+((?:\d{1,2}|100)%|0(?:\.\d+)?|1))?)\)$";
string input = @"rgb(0,0,0)
rgb(255,255,255)
rgb(0%, 255, 255)
rgba(0%, 255, 0, 0)
rgb(0 0 0)
rgb(255 255 255)
rgb(0% 255 255)
rgba(0% 25 0 0)
bad ones
rgb(256,0,0)
rgb(0,0,0)
rgb(255,255,255)
rgb(0% 255, 255)
rgba(%, 255, 0, 0)
rgba(0%, , 0, 0)
rgba(0% 25)
rgb(0 0)
rgb(255 256 255)
rgb(255 255 256)
rgb(0% 255 -1)
rgba(0% 25 0 0 0)";
RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;
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