Here's the code I am currently using. This is intended to HTML-highlight the left-side (search) text of a Perl regex:
my $HighlightText = $CgiArgsRef->{'RawValue'};
$HighlightText =~ s/&/&/go;
$HighlightText =~ s/ / /go; # also use nor
+mal spaces so line wrap is still possible
$HighlightText =~ s/\t/ /go; # also use nor
+mal spaces so line wrap is still possible
$HighlightText =~ s/</</go;
$HighlightText =~ s/>/>/go;
$HighlightText =~ s/\r\n/<br>/go;
# escaped characters
$HighlightText =~ s/(\\\S)/<span class="escapedregexhighlight"
+>$1<\/span>/gso;
my $n = 1;
# groupings like (..)
$HighlightText =~ s/([^\\]?|^)(\()(.+?[^\\])(\))/sprintf("%s<s
+pan class=\"searchregexhighlight\">%s<\/span><span class=\"groupregex
+textcolor\">%s<\/span><span class=\"searchregexhighlight\">%s<\/span>
+<span class=\"parametersup\">\0x00%s<\/b><\/span>",$1,$2,$3,$4,$n++)/
+gsoe;
# single chars like . * ^ $ ? + and aggregates like {} and []
$HighlightText =~ s/([^\\]|^)([\.\+\*\?]+|[\^\$\(]|\)\+?\??\*?
+\{?\d*,?\d*\}?|\[.+?\]\+?\??\*?\{?\d*,?\d*\}?|\{\d*,?\d*\})/$1<span c
+lass="searchregexhighlight">$2<\/span>/gso;
# other characters
$HighlightText =~ s/\\{0}(\\[\w\s\d])/<span class="searchregex
+highlight">$1<\/span>$3/gso;
# put back the '$' in parameter subscripts
$HighlightText =~ s/\0x00/\$/go;
$HighlightText =~ s/[\r\n]+/<br>\n/go;
As mentioned, the main problem is that while this handles basic (a|b|c) groupings, it doesn't do nested groupings like (aaaa(b|c)) or ((?=pretext)aaaa). And because it's using regex to parse regex, there are other things that it doesn't do right when there are many special metacharacters in a row like a{1,3}.*?[def]+.
So, while I was really looking for something better like a do-all Perl module, I'd be happy for any suggestions that you might have to improve my existing code.
thanks!
| [reply] [d/l] |