views:

72

answers:

4

I'm getting started with RegEx and I was wondering if anyone could help me craft a statement to convert coordinates as follows:

145.00694,-37.80421,9 145.00686,-37.80382,9 145.00595,-37.8035,16 145.00586,-37.80301,16

to

145.00694,-37.80421
145.00686,-37.80382
145.00595,-37.8035 
145.00586,-37.80301

(Strip off the last comma and value and turn it into a line break.)

I can't figure out how to use wildcards to do something like that. Any help would be greatly appreciated! Thanks.

+2  A: 
>>> import re
>>> s="145.00694,-37.80421,9 145.00686,-37.80382,9 145.00595,-37.8035,16 145.00586,-37.80301,16"
>>> print re.sub(",\d*\w","\n",s)
145.00694,-37.80421
145.00686,-37.80382
145.00595,-37.8035
145.00586,-37.80301
gnibbler
Works great! Thanks so much!
John
And this makes another great example of how fragile regex are: This only works as long as the 2nd number (-37.X) is negative ...
THC4k
Yeah I just noticed that...how would I modify it to make it match ",[two digit number][white space]"? I'm trying ",\d\d\w" but that doesn't seem to work.
John
Got it. ",\d\d\s"
John
+3  A: 

"Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems." --Jamie Zawinski

Avoid that problem and use string methods:

s="145.00694,-37.80421,9 145.00686,-37.80382,9 145.00595,-37.8035,16 145.00586,37.80301,16"

lines = s.split(' ') # each line is separated by ' '
for line in lines:
    a,b,c=line.split(',') # three parts, separated by ','
    print a,b

Regex have their uses, but this is not one of them.

THC4k
+1  A: 

String methods seem to suffice here, regex are overkill:

>>> s='145.00694,-37.80421,9 145.00686,-37.80382,9 145.00595,-37.8035,16 145.00586,-37.80301,16'
>>> print('\n'.join(line.rpartition(',')[0] for line in s.split()))
145.00694,-37.80421
145.00686,-37.80382
145.00595,-37.8035
145.00586,-37.80301
SilentGhost
A: 
>>> s = '145.00694,37.80421,9 145.00686,-37.80382,9 145.00595,-37.8035,16 145.00586,-37.80301,16
>>> patt = '(%s,%s),%s' % (('[+-]?\d+\.?\d*', )*3)
>>> m = re.findall(patt, s)   
>>> m
['145.00694,37.80421', '145.00686,-37.80382', '145.00595,-37.8035', '145.00586,-37.80301']
>>> print '\n'.join(m)
145.00694,37.80421
145.00686,-37.80382
145.00595,-37.8035
145.00586,-37.80301

but I prefer not use regular expressions in this case

I like SilentGhost solution

remosu