Perl: regular expression to check for ANSI sequences
I needed to test if a given string contains ANSI color codes. Here is the regexp I settled on to check for that:
my $ansi_color_regex = qr/\e\[[0-9]{1,3}(?:;[0-9]{1,3}){0,3}[mK]/;
if ($str =~ /$ansi_color_regex/) {
print "String has some ANSI in it\n";
}
Alternately you can capture the color numbers with this regex:
my $ansi_color_regex = qr/(\e\[([0-9]{1,3}(;[0-9]{1,3}){0,3})[mK])/;