Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: RegEx Riddle

by reasonablekeith (Deacon)
on May 28, 2008 at 10:12 UTC ( [id://688826]=note: print w/replies, xml ) Need Help??


in reply to Re: RegEx Riddle
in thread RegEx Riddle

I'd take a different tack, and loop through the placemark nodes.

I've also assumed the OP wants all coords, rather than the last one as per your example, so have dumped them in a hash of arrays...

my $xml = 'your xml...'; my %coords_by_name; while ($xml =~ m{<Placemark>(.*?)</Placemark>}gs ) { my $placemark_snippet = $1; if ( $placemark_snippet =~ m{(<name>.*?</name>)}gs ) { my $name = $1; while ( $placemark_snippet =~ m{(<coordinates>.*?</coordinates +>)}gs ) { push @{$coords_by_name{$name}}, $1; } } } use Data::Dumper; print Dumper(\%coords_by_name);
I agree with the other comments though, you need a good reason to not use a proper xml parser, the above code won't be very robust, and only supports a fraction of the valid ways you could write that xml.
---
my name's not Keith, and I'm not reasonable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://688826]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found