Reputation: 451
I wanna extract some latitude and longitude values from a KML file, which looks like :
<coordinates>0.197118,48.003719,0.000000</coordinates>
// ...
<coordinates>
0.197120,48.003719,0.000000
0.197060,48.003792,0.000000
0.198390,48.004059,0.000000
</coordinates>
// ...
<coordinates>0.196763,48.003162,0.000000</coordinates>
So you can see in the text the format of coordinates is :
(space*)longitude,latitude,altitude\n // or
(space*)<coordinates>longitude,latitude,altitude</coordinates>\n
So to catch latitudes, it's ok, I matched :
@".*?,(.*?),.*?$" // matching index 1
And for longitudes, I used the pattern :
@" * (<coordinates>)? (.*?),(.*?),(.*?)$" // matching index 2
I tried many things including this last pattern, but I always get as matches :
"<coordinates>0.197118",
"0.197120"
"0.197060"
"0.198390"
"<coordinates>0.196763"
And you got it : I don't want <coordinates>
.
What's wrong in @" * (<coordinates>)? (.*?),(.*?),(.*?)$"
?
Thanks.
Upvotes: 0
Views: 196
Reputation: 451
Ok so bad move by me here, just one more space standing :
Instead of :
@" * (<coordinates>)? (.*?),(.*?),(.*?)$"
I had to do :
@" * (<coordinates>)?(.*?),(.*?),(.*?)$"
That fix the problem ! No need of xml parser or something else for that…
Upvotes: 1
Reputation: 530
If you really want to use regexp, try a non-greedy: @".*?". After matching you should iterate through the results and remove tags. The string left can be devided by @","
Upvotes: 0
Reputation: 41958
You're overcomplicating it with all the greediness modifiers, look at my sample here that just works:
(([0-9\.]+),([0-9\.]+),([0-9\.]+)[\w,]+)+
Also, I do agree that you shouldn't be parsing XML with regexps in general, but it can be an acceptable quick hack if it greatly shortens your code and you don't need anything else from the source file.
Upvotes: 0