Don't want to do the regexp test twice

Irmen de Jong irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Thu Jul 24 18:12:43 EDT 2003


Egbert Bouwman wrote:

> 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.

I'd do it like this:

pat = re.compile(r'...')
for line in mylist:
     if ... :
         ....
     elif ... :
         ....
*** insert all the ifs that don't need the regexp ***
         ....
     else:
         mat = pat.search(line)
         if mat:
              ....do stuff because pattern does match.....
         else:
              ....everything else....


HTH,
Irmen de Jong





More information about the Python-list mailing list