tags:

views:

94

answers:

5

So I am trying to read an XML file into a string in Perl and send it as part of a SOAP message. I know this is not ideal as there are methods for SOAP sending files, however, I am limited to having to use the SOAP that is set up, and it is not set up for sending with file support.

Therefore I need to parse out the markup tags <> and replace them with []. What is the best way to do this?

A: 

I don't have Perl handy, but might this work?

$string =~ s/</[/;
$string =~ s/>/]/;

PS: You shouldn't use regex for this. Please. For the baby seals' sake.

David Titarenco
I actually started with s/\</\[/ and flip, but this seemed to only replace the first instance.
bmucklow
@bmucklow - you need a 'g' at the end of your expression, to replace all instances globally, not just the first.
ire_and_curses
use the g modifier:$string =~ s/</[/g
Adam Rabung
+2  A: 

If simply replacing < by [ and > by ] isn't working for you (perhaps because angle brackets are showing up in CDATA sections somewhere that you don't want replaced), then you probably won't profit much from regexes here. Regular expressions are not suited for matching non-regular languages like XML.

You might get away with searching for <([^>]+)> and replacing that with [$1]:

$subject =~ s/<([^>]+)>/[$1]/g;
Tim Pietzcker
+2  A: 

Will something simple like this work for you?

$a=~y/<>/[]/;

y performs a one to one substitution. < becomes [, and > becomes ]. The perlop documentation explains it in greater detail.

Robert Wohlfarth
This actually worked, however it didn't fix the issues I'm getting trying to send my SOAP message. Thanks though.
bmucklow
+2  A: 

What about using Base64 instead?

leonbloy
+1  A: 

Won't somebody please think of the baby seals?

As others have already pointed out, both in answers and in comments, doing this with a regex will cause problems as soon as your data becomes sufficiently complex to include either [/] or </> characters. Once that happens, any simple regex will break and you'll need to either duct tape it back together in hopes that it'll limp along a bit longer before breaking again or re-implement it with a real XML parser and/or a better SOAP implementation.

OTOH, leonbloy's suggestion of base64 encoding your data is actually a pretty good one. I hadn't thought of that and it should work just as well as a proper SOAP implementation, with the caveats that the sent data will be larger and, if you need to do wire-level debugging, it may be more difficult to interpret the content.

Dave Sherohman