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

Replies are listed 'Best First'.
Re^3: Regex to replace a particular part of content
by GrandFather (Saint) on Jun 23, 2006 at 18:10 UTC

    Now do it when you don't know what the text is up front and the text varies from line to line.

    With this version you might as well just have used a print statement containing the litteral output. I'm sure in OP's real problem the text is not as shown. A better sample may have comprised words like "foo bar baz" and been much shorter, but there were enough lines there that you should probably have been able to read between them. :).


    DWIM is Perl's answer to Gödel
      If it was a real world problem with real HTML/XML/whatever you would recommend some sort of parser rather than regular expressions. However, the OP asked for a regular expression (misguidedly) to modify a piece of unrealistic text (I can read between the lines as well as the next man, who's ever seen real world mark-up like that?:-). I've had some fun and learnt a few things trying to see if it could be done that way so I'm happy.

      Cheers,

      JohnGG