http://qs321.pair.com?node_id=557197


in reply to Re: Regex to replace a particular part of content
in thread Regex to replace a particular part of content

Here is a single regex that seems to do the trick. It does it by using look-around assertions before and after the "The quick ... dog. " phrase we want to modify and by using alternation. Here is the code

use strict; use warnings; my $qbf = q{The quick brown fox jumps over the lazy dog. }; (my $qbfForRX = $qbf) =~ s{\s}{\\s}g; my $message = $qbf x 7 . qq{\n<list>} . ($qbf. qq{\n}) x 2 . $qbf . qq{</list>\n} . $qbf x 6 . qq{\n}; print $message, qq{\n}; my $rxToChange = qr {(?x) # Use extended syntax (?: # Non-capture group # for alternation # Either ... (?<=\n)(?=$qbfForRX) # Move engine to point # preceded by newline # and followed by # phrase | # ... or ... (?<=<list>)(?=$qbfForRX) # Move engine to point # preceded by <list> # and followed by # phrase ) # Close non-capture # group ($qbfForRX) # Capture phrase (?=(?:\n|</list>)) # If followed by either # newline or </list> }; $message =~ s{$rxToChange}{<item>$^N</item>}g; print $message;
and when run it produces

The quick brown fox jumps over the lazy dog. The quick brown fox jumps + over the lazy dog. The quick brown fox jumps over the lazy dog. The +quick brown fox jumps over the lazy dog. The quick brown fox jumps ov +er the lazy dog. The quick brown fox jumps over the lazy dog. The qui +ck brown fox jumps over the lazy dog. <list>The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. </list> The quick brown fox jumps over the lazy dog. The quick brown fox jumps + over the lazy dog. The quick brown fox jumps over the lazy dog. The +quick brown fox jumps over the lazy dog. The quick brown fox jumps ov +er the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps + over the lazy dog. The quick brown fox jumps over the lazy dog. The +quick brown fox jumps over the lazy dog. The quick brown fox jumps ov +er the lazy dog. The quick brown fox jumps over the lazy dog. The qui +ck brown fox jumps over the lazy dog. <list><item>The quick brown fox jumps over the lazy dog. </item> <item>The quick brown fox jumps over the lazy dog. </item> <item>The quick brown fox jumps over the lazy dog. </item></list> The quick brown fox jumps over the lazy dog. The quick brown fox jumps + over the lazy dog. The quick brown fox jumps over the lazy dog. The +quick brown fox jumps over the lazy dog. The quick brown fox jumps ov +er the lazy dog. The quick brown fox jumps over the lazy dog.

You were right when you said "You can't do it easily ...". Problems like this would become easier if look-behind assertions were able to accept variable width patterns. I wasted some time trying to work around that before thinking of moving the alternation outside the look-behind. I wasted even more time because I was interpolating the phrase directly into the regex not realising that the 'x' flag was eating the spaces in it. Doh!

Cheers,

JohnGG