I have fuzzy memory about backreferences in character classes. I recall hearing that they work in quite new versions of Perl and then being straightened out that they don't work and likely never will. That is, that [^\1] always matches any character but "\1" (CTRL-A, "\cA"). You seem to think that [^\2] works and I'm not trying to say you are wrong. Just that you might be wrong.

So, instead of [^\2] you might want (?!\2) (which wouldn't be the same if $2 were more than a single character).

Also, <[^<>]+> is better, otherwise:

Well, if < _$20_, you *need* <a href="...">this</a>
won't expand either of the _ and * formatting. (Below I took the liberty of turning such unmatched <s into &lt; as well.)

In a situation like this, I'd "tokenize" rather than "match":

use warnings; use strict; sub reg_fix { my $text = shift(@_); # I guessed what you wanted =equals= to indicate: my %tag= qw( / em * b _ u = strike ); my %open; @open{ qw( / * _ = ) }= (); my @tokens= $text =~ m{ ( <[^<>]+> | [/*_=] | [^</*_=]+ | < ) }gmx; my @nested; for my $token ( @tokens ) { if( "<" eq $token ) { $token= "&lt;"; } elsif( exists $open{$token} ) { if( ! defined $open{$token} ) { $open{$token}= \$token; push @nested, $token; } else { ${$open{$token}}= "<$tag{$token}>"; my $nest; do { $nest= pop @nested; undef $open{$nest}; } while( $nest ne $token ); $token= "</$tag{$token}>"; } } } return join '', @tokens; } print reg_fix($_) for <DATA>; __END__ go/to <img src='http://allpoetry.com:8080/images/smile/happy.gif'> /th +x/ <img src='http://allpoetry.com:8080/images/smile/happy.gif'> _sill +y *to_* Well, if < _$20_, you *need* <a href="...">this</a>
which produces:
go<em>to <img src='http://allpoetry.com:8080/images/smile/happy.gif'> +</em>thx/ <img src='http://allpoetry.com:8080/images/smile/happy.gif' +> <u>silly *to</u>* Well, if &lt; <u>$20</u>, you <b>need</b> <a href="...">this</a>

Note how I prevent mis-nesting of attributes.

                - tye

In reply to Re: match *bold* formatting, but avoid html (tokenize) by tye
in thread match *bold* formatting, but avoid html 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.