[OSM-dev] Split osm line with perl

Lennard ldp at xs4all.nl
Sun Nov 29 11:41:04 GMT 2009


Maarten Deen wrote:

> I've tried a few things, but I'm not fluent in perl. My problem at the moment is 
> that splitting a line on the space character seems logical, but you run into 
> problems if a value has a space in it.
> So splitting something like <tag k="name" v="foo bar"/> will split the value 
> "foo bar" also.

You have to be fluent in regexes, not perl as such. The trick is to 
match the quote, then to match anything that is not a quote, followed by 
a quote.

#!/usr/bin/perl

my $str = "<tag k=\"name\" v=\"foo bar\"/>";

$str =~ /k="([^"]*)" v="([^"]*)"/;
my ($k, $v) = ($1, $2);

print "k = '$k', v = '$v'\n";


Difficulty: are values with quotes allowed in k/v pairs?


-- 
Lennard




More information about the dev mailing list