Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Your solution does not work. Initially, it will appear to work against his data set, but XML start and end tags don't have to appear on the same line. If that happens, your regex will break because the dot metacharacter doens't match the newline. Adding the /s modifier allows the dot to match, but then, because your match is greedy, it still breaks:

#!/usr/bin/perl use strict; my @buf = <DATA>; for my $i ( 0 .. $#buf ) { if ($buf[$i] =~ s/^\s*<jobnumber>(.*)<\/jobnumber>\s*$/$1/s) { $buf[$i+1] =~ s/^\s*<location>\s*(.*)\s<location>\s*$/$1/s; # if your tags are really like this &process($buf[$i],$buf[$i+1]); } } sub process { my ($jobnumber,$location) = @_; print "Found a job $jobnumber in $location.\n"; # do something } __DATA__ <posts> <post> <jobnumber> 1234 </jobnumber> <location>Somecity, NJ</location> </post> <post> <jobnumber>87922</jobnumber> <location>Othercity, AK</location> </post> </posts>

See Death to Dot Star! for the explanation of why your regex fails (and for some excellent examples of how I have screwed up regexes on delimited text).

Use a parser for data like this. Regexes, while I love them, are for matching data, not parsing it.

As for your 'related note', it doesn't work because you have (.?) in your code. The dot/question mark makes you match one character and have that match optional. It's equivalent to (.{0,1}).

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid - don't use regexes for parsing) Re(2): Parsing XML by Ovid
in thread Pasring XML into a simple hash by Desdinova

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2020-05-28 22:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If programming languages were movie genres, Perl would be:















      Results (166 votes). Check out past polls.

      Notices?