Regex help

Status
Not open for further replies.

madjoe

Ars Scholae Palatinae
735
Subscriptor
Hello all,

I have the following text (all on the same line):
Code:
180.00,1,185.00,35,190.00,122,192.00,30,192.50,8,193.00,23,193.50,0,194.00,90,194.50,91,195.00,260,195.50,10,196.00,91,196.50,217,197.00,124,197.50,12,198.00,773,198.50,102,199.00,491,199.50,251,200.00,1065,200.50,555,201.00,838,201.50,1832,202.00,1237,202.50,2192,203.00,2495,203.50,1415,204.00,1405,204.50,2180,205.00,3978,205.50,4831,206.00,5447,206.50,2729,207.00,5900,207.50,3312,208.00,8487,208.50,4478,208.80,7,209.00,10739,209.50,5099,210.00,23449,210.50,5977,211.00,59360,211.50,13462,212.00,13171,212.50,15931,213.00,18177,213.50,19773,214.00,5668,214.50,3760,215.00,41943,215.50,5613,216.00,2582,216.50,1949,217.00,5490,217.50,2856,218.00,3166,218.50,2252,219.00,1522,219.50,10,220.00,2541,222.50,3779,225.00,4293,227.50,2584,230.00,10921,232.50,3201,235.00,29,237.50,1318,240.00,10,175.00,19361,180.00,12777,185.00,25723,189.00,959,189.50,644,190.00,13992,190.50,959,191.00,342,191.50,44,192.00,1763,192.50,19280,193.00,838,193.50,2715,194.00,3483,194.50,2584,195.00,7698,195.50,2773,196.00,2725,196.50,2211,197.00,2599,197.50,9471,198.00,52926,198.50,3211,199.00,7091,199.50,3242,200.00,29001,200.50,2029,201.00,15542,201.50,4715,202.00,25876,202.50,9145,203.00,42741,203.50,3402,204.00,24342,204.50,5918,205.00,61185,205.50,7675,206.00,36681,206.50,24690,207.00,16062,207.50,6575,208.00,30383,208.50,6694,208.80,3,209.00,10736,209.50,6833,210.00,19954,210.50,2774,211.00,2513,211.50,268,212.00,1113,212.50,159,213.00,332,213.50,106,214.00,232,214.50,72,215.00,32372,215.50,29,216.00,23,216.50,40,217.00,2,217.50,48,218.50,1,219.00,80,220.00,91,225.00,14

I'm trying to replace every other comma (,) with a new line character (\n). So it looks like this:
Code:
180.00,1
185.00,35
190.00,122
etc...

I'm trying to do this with regex:
Code:
(,(?:(?!,).)*),((?:(?!,).)*)

This is the substitution:
Code:
\1\n\2

This yields the first number (180.00) followed by a new line character for each number (each line is blank) and the last number with a comma preceding it (,14).

Check it out:
Code:
180.00





































































































































,14

Any idea what I'm doing wrong? I'm trying to do this in python:
Code:
parsedtext = re.sub(r"(,(?:(?!,).)*),((?:(?!,).)*)", "\1\n\2", textfield)

Thank you!
 

Fruity Pebbles

Ars Praetorian
595
Subscriptor
[url=http:/civis/viewtopic.php?p=30199491#p30199491:uqqfxwd5 said:
CanSpice[/url]":uqqfxwd5]Do you really need to use a regular expression? Why not just split the string on commas, then loop through your array?

I like that idea.

Code:
a = the_string.split(',')
i = 0
while i < len(a):
  print "%s,%s" % (a[i], a[i+1])
  i += 2
 

madjoe

Ars Scholae Palatinae
735
Subscriptor
Some good solutions here. I appreciate the help guys! Thanks for pointing me in the right direction.

Below is what worked for me. I haven't tried the splitting suggestion yet. I had to use raw strings in the substitution part (r"\1\n" as opposed to "\1\n") for it to work. This was throwing me off.

Code:
parsedtext = re.sub(r"(\d+\.\d+,\d+)(,)", r"\1\n", rawtext)
 

zeotherm

Ars Legatus Legionis
10,960
Moderator
[url=http:/civis/viewtopic.php?p=30203529#p30203529:t0c1328x said:
madjoe[/url]":t0c1328x]Some good solutions here. I appreciate the help guys! Thanks for pointing me in the right direction.

Below is what worked for me. I haven't tried the splitting suggestion yet. I had to use raw strings in the substitution part (r"\1\n" as opposed to "\1\n") for it to work. This was throwing me off.

Code:
parsedtext = re.sub(r"(\d+\.\d+,\d+)(,)", r"\1\n", rawtext)
As soon as you get a number without a decimal place, this will fail. Or one in scientific notation. Or one with digits only AFTER the decimal. Or one where there is a decimal in the second number.
 

madjoe

Ars Scholae Palatinae
735
Subscriptor
My saga continues... I'm able to create a string like the following (called "parsedtext"):

Code:
175.00,13495
176.00,11122
177.00,5074
178.00,1625
180.00,21113
181.00,27
182.00,2347
183.00,3408
184.00,3061
185.00,25689
186.00,71
187.00,1643
188.00,281
189.00,648
190.00,22426

But now need to parse out the first line and the last line and then eventually parse out just the number with the decimal from each. I'm trying to use the following:

Code:
firstline = re.sub("([0-9]+\.[0-9][0-9])", r"\1", parsedtext, flags=re.M)
Code:
lastline = re.sub("^.*(?![\n])$", r"\1", parsedtext, flags=re.M)

But I'm not having any luck. Re.M is the MULTILINE flag for re.sub. After some research it seems this is required or else every line will be matched which isn't what I want. I've also played around with re.compile and then re.match/re.search without any luck although I don't think these are necessary to do what I want. I'm using Python 2.7.

Any thoughts? I'm sure this is far simpler than I'm making it out to be.

Thanks
 

calpan

Ars Scholae Palatinae
1,356
Subscriptor
You can do this instead:
Code:
s = '180.00,1,185.00,35,190.00,122,192.00,30,192.50,8,193.00,23,193.50,0'.split(',')
pairs = zip(s[::2], s[1::2])
first = pairs[0]
last = pairs[-1]

If you just want the first number of the first and last pair, this will work:
Code:
print first[0]
print last[0]

Is there any reason you need to use a regular expression?
 
Status
Not open for further replies.