use strict;
my $str = '// 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)
';
my $regex = qr/^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]+))?)\)$/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
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 Perl, please visit: http://perldoc.perl.org/perlre.html