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/&/&amp;/go; $HighlightText =~ s/ /&nbsp; /go; # also use nor +mal spaces so line wrap is still possible $HighlightText =~ s/\t/&nbsp; &nbsp; &nbsp;/go; # also use nor +mal spaces so line wrap is still possible $HighlightText =~ s/</&lt;/go; $HighlightText =~ s/>/&gt;/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!

In reply to Re^2: good syntax highlighter for regexp? by ManFromNeptune
in thread good syntax highlighter for regexp? by ManFromNeptune

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.