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

In reply to search and replace is not working as expected... by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.