R
Rikard Bosnjakovic
I'm tidying up some code. Basically, the code runs a bunch of
regexp-searches (> 10) on a text and stores the match in a different variable.
Like this:
re1 = r' ..(.*).. '
re2 = r' .... '
re3 = r' .(.*).. '
...
m = re.search(re1, data)
if m:
myclass.bar = m.group(1)
m = re.search(re2, data)
if m:
myclass.foo = m.group(1)
m = re.search(re3, data)
if m:
myclass.baz = m.group(1)
While this code works, it's not very good looking.
What I want is to rewrite it to something like this:
l = [ (re1, myclass.bar),
(re2, myclass.foo),
(re3, myclass.baz),
]
for (x,y) in l:
m = re.search(x, y)
if m:
y = m.group(1)
But since Python doesn't work that way, that idea is doomed. What I'm
looking for are other (better) ways or pointers to accomplish this task of
cleanup.
regexp-searches (> 10) on a text and stores the match in a different variable.
Like this:
re1 = r' ..(.*).. '
re2 = r' .... '
re3 = r' .(.*).. '
...
m = re.search(re1, data)
if m:
myclass.bar = m.group(1)
m = re.search(re2, data)
if m:
myclass.foo = m.group(1)
m = re.search(re3, data)
if m:
myclass.baz = m.group(1)
While this code works, it's not very good looking.
What I want is to rewrite it to something like this:
l = [ (re1, myclass.bar),
(re2, myclass.foo),
(re3, myclass.baz),
]
for (x,y) in l:
m = re.search(x, y)
if m:
y = m.group(1)
But since Python doesn't work that way, that idea is doomed. What I'm
looking for are other (better) ways or pointers to accomplish this task of
cleanup.