(This is all a simplified case of what I'm actually trying to do.)
If you are actually trying to do something more complicated than this, and if everything you're doing involves HTML or XML markup, then for the sake of your own sanity (and that of anyone else who might look at or work with your code), use an appropriate parser module (HTML::TokeParser, XML::Parser, or simple variants thereof) -- you'll see this advice repeated often at the monastery, and with good reason. Once you grok using one of these modules, the job will be easier, and there won't be any risk of duplicating tags by mistake with a regex.

update: I thought about posting some sample code, but decided not to, because you say the given example isn't exactly what you're really trying to do, and because the given example in itself doesn't really make sense -- it would seem more sensible if the intended output looked like this:

<p> <b> blah blah <blockquote> blah </blockquote> blah </b> </p>
or like <b> <p> ... </p> </b> -- and when you use a parser module, this is the sort of modification that would be both natural and trivial to do.

In reply to Re: Regex: How do I use lookahead with search/replace? by graff
in thread Regex: How do I use lookahead with search/replace? by tunesmith

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.