Don't want to do the regexp test twice

Mike Rovner mike at nospam.com
Thu Jul 24 18:02:21 EDT 2003


Egbert Bouwman wrote:

> 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) ?

for line in mylist:
    if ... :
        ....
    elif ... :
        ....
    else:
        mat = pat.search(line)
        if mat:
           ...
        elif ... :
           ...
        else ...:
           ...

Mike








More information about the Python-list mailing list