I want to change this data:
<p> blah blah <blockquote> blah </blockquote> blah <p>
into this:
<p> <b>blah blah</b> <blockquote> <b>blah</b> </blockquote> <b>blah</b> <p>
I tried:
s/(<.*?>)(.*?)(<.*?>)/$1<b>$2</b>$3/g;
But that didn't touch the code inside of <blockquote> because the marker had already gone past that tag on the first match.

I switched to lookahead/lookback:

s/(?=(<.*?>)(.*?)(<.*?>))/$1<b>$2</b>$3/g;
But that created extra code; a nested blockquote for example.

(This is all a simplified case of what I'm actually trying to do.) In summary: How do I do search/replace if a pattern matches both the end of one and the beginning of another string to replace? Alternative solutions: What is the best way to either tell the "replace" part to only print out chunks of code it hasn't printed before, OR, how can I add additional search criteria to the "search" part without it becoming part of the replacement? Like, "search for this whole string, but only replace this portion of that string with this other replacement string".

Thanks, tunesmith


In reply to 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.