$re = '/^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))?)\)$/mi';
$str = '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)';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
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 PHP, please visit: http://php.net/manual/en/ref.pcre.php