http://qs1969.pair.com?node_id=214489

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

We are using a tiny perl script that receives an xml "string" as it's argument, which then creates an xml file from the string it receives so that it can be passed on to our workflow server. The problem is that we are getting some odd characters in one of our incoming xml fields that I would like the perl script to get rid of before it creates the xml. Here is what I have:
#get rid of the bad doct characteres if ($ARRGH =~ /<className>ibZtfacr008_chg<\/className>/) { $ARRGH =~ s/<doct>Customer trans\.(.*)<\/doct>/<doct>Customer tran +s\.<\/doct>/g; }
and as an example, here's what $ARRGH would contain:
<event> <className>ibZtfacr008_chg</className> <oldvalue> <x>foo</x> <y>bar</y> <doct>Customer trans. garbage</doct> </oldvalue> <newvalue> <x>bar</x> <y>foo</y> <doct>Customer trans. garbage</doct> </newvalue> </event>

This looks fine to me, but what I end up getting is that it finds the first doct tag, finds the Customer Trans. string, but then it ignores the first (matching) closing doct tag and goes all the way to the newvalue's closing doct tag; deleting everything in between, instead of just replacing both instances of doct with the correct information. And to me it seems like i have to use the (.*) parameter because the length of the garbage that I want to get rid of varies in that tag, but I know that has to be what's causing my problem.

Now I'm a complete newb when it comes to perl, as I don't use it whatsoever, except for in this case (this is my first and only perl script :) So please if you answer the question be very explicit in the details of what to change so that I know what you're doing. Thanks!

Brian Newtz

Replies are listed 'Best First'.
Re: search and replace is not working as expected...
by pg (Canon) on Nov 20, 2002 at 15:54 UTC
    your (.*) should be (.*?), otherwise it will include <\/doct>, which is not what you expected. ? simply make the s/// more lazy.
Re: search and replace is not working as expected...
by mikeirw (Pilgrim) on Nov 20, 2002 at 16:20 UTC

    I believe you want this:

    $ARRGH =~ s#<doct>Customer trans\.(?:.*)+?</doct>#<doct>Customer trans\.</doct +>#g;

    What the +? does is makes the match less "greedy", so that it matches the first </doct> it finds instead of the last. Also, since you aren't using what you capture in (.*), I put the ?: in, so that perl doesn't try to retain what it found, but still keeps the grouping.

    I also changed the regex delimiter to | so that you don't have to escape the / in </doct>.

    Update: As per Aristotle's reply, I've changed the regex delimiter to #.

      I also changed the regex delimiter to | so that you don't have to escape the / in </doct>.
      Yuck, please use something else next time. | is a regex metacharacter, you just opened a can of worms.

      Makeshifts last the longest.

Re: search and replace is not working as expected...
by chromatic (Archbishop) on Nov 20, 2002 at 17:35 UTC

    Would using an actual XML::Parser translate these "garbage" characters from encoded entities into actual characters, or do you need to fix the previous program in the chain so it produces real XML? (Related link: XML::Simple)

      Thanks everyone for the quick replies. I basically just ended up doing:
      $ARRGH =~ s/<doct>Customer trans\.(.*?)<\/doct>/<doct>Customer trans\. +<\/doct>/g;
      as per the first suggestion and that one worked great. Thanks a lot!