Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Regex to replace a particular part of content

by GrandFather (Saint)
on Jun 22, 2006 at 11:18 UTC ( #556888=note: print w/replies, xml ) Need Help??


in reply to Regex to replace a particular part of content

You can't do it easily with a single regex, but you can do it with a modest amount of code:

use strict; use warnings; while (<DATA>) { if (m|<list>| .. m|</list>|) { s/^(?!<list>)|(?<=<list>)/<item>/g; s-(?=</list>)|(?<!</list>)(?=\n)-</item>-g; } print; } __DATA__ The first 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.</list> The last quick brown fox jumps over the lazy dog.

Prints:

The first quick 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 last quick brown fox jumps over the lazy dog.

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Regex to replace a particular part of content
by johngg (Canon) on Jun 23, 2006 at 14:22 UTC
    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

    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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://556888]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-08-16 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?