Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Perl RE; how to capture, and replace based on a block?

by GrandFather (Saint)
on Dec 18, 2013 at 00:23 UTC ( [id://1067555]=note: print w/replies, xml ) Need Help??


in reply to Perl RE; how to capture, and replace based on a block?

Don't do that. As a general thing correctly parsing and editing XML is tricky. Use a module such as XML::Twig to do the hard work for you.

If you'd provided sample code, input data and expected output most likely someone would show you how to use an appropriate module to do the job.

True laziness is hard work
  • Comment on Re: Perl RE; how to capture, and replace based on a block?

Replies are listed 'Best First'.
Re^2: Perl RE; how to capture, and replace based on a block?
by taint (Chaplain) on Dec 18, 2013 at 00:33 UTC

    Ahh. I see. I didn't have any code (other than the RE I was using). Because, at this point, if I can't even match the block. There would be no point in attempting to replace. So I hadn't bothered to attempt to replace anything yet. I'm still trying to figure out how to correctly match what I need.

    Just seemed the logical progression, in learning to do it. :)

    Thanks GrandFather, for the reply (and suggestion).

    --Chris

    Yes. What say about me, is true.
    

      I agree that use of an XML parser is likely to be a better idea, but just an example of what your fellow monks were hoping for as an example of what you tried and what resulted (except this works):

      >perl -wMstrict -le "my $s = qq{xxx </div>\n</body> xxx}; print qq{[[$s]]}; ;; my $tags = qr{ </div> \n </body> }xms; $s =~ s{ $tags }{gone}xms; print qq{[[$s]]}; " [[xxx </div> </body> xxx]] [[xxx gone xxx]]

      Question: Are you sure it's only a single newline that's present? The presence of other whitespace characters than just a newline can confuse the issue. The following might be a better regex:
          qr{ </div> \s* </body> }xms

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-24 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found