Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: RegEx Riddle

by prasadbabu (Prior)
on May 28, 2008 at 05:47 UTC ( #688803=note: print w/replies, xml ) Need Help??

in reply to RegEx Riddle

Hi ferddle,

You can use negative look ahead regex to accomplish your job. If you have whole file in a string $kml, then

use strict; use warnings; use Data::Dumper; my $kml = do { local $/, <DATA>}; my %hash; while ($kml =~ m/<name>((?:(?!<name>).)*)<\/name>\s*(<coordinates>(?:( +?:(?!<coordinates>).)*)<\/coordinates>)/gs){ $hash{$1} = $2; } print Dumper \%hash; output: ------- $VAR1 = { 'One more' => '<coordinates>56,78,0</coordinates>', 'This is the title' => '<coordinates>12,34,0</coordinates>' };

You can learn more about positive look ahead at perlre.

updated, added code. Thanks to ikegami for pointing out the mistake.


Replies are listed 'Best First'.
Re^2: RegEx Riddle
by reasonablekeith (Deacon) on May 28, 2008 at 10:12 UTC
    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?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2020-05-29 17:10 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (170 votes). Check out past polls.