I am a beginner to python regexes. I achieved what I needed but it is really ugly as I am missing experience. My goal is to convert an array of strings of the form:
notes = ["10.0% higher", "5.0% lower", "Same as", "21.2% lower"]
to an array of floats, so that the above array yields:
changes = [10.0,-5.0,0,-21.2]
The code below achieves that but is really repetitive and bad style. How can I optimize that?
changes = []
for note in notes:
m = re.search(r"(?:(\d+\.\d+\%\shigher)|(\d+\.\d+\%\slower)|(Same\sas))", note)
if m:
if m.groups(0):
if m.groups(0)[0]:
changes += [float(re.match(r"(\d+\.\d+)", m.groups(0)[0]).groups(0)[0])]
elif m.groups(0)[1]:
changes += [-float(re.match(r"(\d+\.\d+)", m.groups(0)[1]).groups(0)[0])]
else:
changes += [0.0]
print changes