I've got a script which essentially restricts HTML within a post to certain tags (much like many sites), and automatically adds 'BR' tags to new lines. One of the tags I'd like to allow is the 'PRE' tag, which should not have a break tag to break lines.

I use the regex 's/\n/<BR>\n/gs' to add the break tags to the whole comment, then I'd like to remove the tags in PRE by using something like:

while(/&lt;pre>(.*?)&lt;\/pre>/) { $1 =~ s/<BR>\n/\n/gs; }

Naturally this doesn't work, but how does one perform a substitute on a match in such a situation?

This is just one example of this; another time I've come across this problem is using perl to highlight code syntax on a DOS box using ANSI escape sequences: I would have like to be able to remove the escape sequences that appear within comments or quotes, so something like (off the top of my head):

while(/"(.*?)"/g) { $1 =~ s/\c@\[\d+[a-x]//g; }

Thanks!


In reply to Substitute within a search by Anonymous Monk

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.