in reply to regex and XML

Another bit of the puzzle is that in the example you have give, the regular expression is being quoted using pipe symbols (|) instead of the usual forward slash. (Perl allows any non alphanumeric character to be used to quote a regular expression, if it starts with an explicit m).

As others have said, this is an unreliable way to parse regular expressions for a number of reasons. It is much better to use an existing XML parsing library from CPAN.

Replies are listed 'Best First'.
Re^2: regex and XML
by elef (Friar) on Dec 13, 2010 at 14:43 UTC
    Another bit of the puzzle is that in the example you have give, the regular expression is being quoted using pipe symbols (|) instead of the usual forward slash.

    Whoever wrote the regex probably did this to avoid having to escape the / in the closing tag. Personally, I always use /, so I would have written $xml =~ /<$fieldname>(.*?)<\/$fieldname>/i

    Talking about parsing XML, what easy-to-use XML parser would the monks recommend? From the quick look I have taken at the documentation of a couple of XML parsers, it would take a week for me to figure out how to do something like "print out the content of every XYZ tag in the document, each one on a new line".
      If you just want to extract certain elements, I'd say XPath is the way to go. There is XML::XPath and XML::XPath::Simple.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James