Don't want to do the regexp test twice
Egbert Bouwman
egbert.list at hccnet.nl
Thu Jul 24 17:44:57 EDT 2003
While looping over a long list (with file records)
I use an (also long) if..elif sequence.
One of these elif's tests a regular expression, and
if the test succeeds, I want to use a part of the match.
Something like this:
pat = re.compile(r'...')
for line in mylist:
if ... :
....
elif ... :
....
elif pat.search(line):
mat = pat.search(line)
elif ... :
...
else ...:
...
Is there a way to to do this job with only one pat.search(line) ?
Of course I can do this:
for line in mylist:
mat = pat.search(line)
if ...:
....
elif ...:
....
elif mat:
...
but the test is relevant in only a relatively small number of cases.
And i would like to know if there exists a general solution
for this kind of problem.
egbert
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
========================================================================
More information about the Python-list
mailing list