matching multiple regexs to a single line...

Alexander Sendzimir lists at battleface.com
Tue Nov 12 19:19:12 EST 2002


Trent,

Nice response. Thanks. This is more in line with what I'm looking for. I
will work with this and see what happens.

Thanks again. And again to John.

Alex



On Tue, 12 Nov 2002 14:21:48 +0000, Trent Mick wrote:
> A slight mod on John's code makes it seem pretty clean to me:
> 
> patterns = [re.compile('...'),
>             re.compile('...')]
> 
>     for line in myfile:
>         for pattern in patterns:
>             match = pattern.match(line)
>             if match:
>                 // do something with 'match'
>                 break
>         else:
>             raise "none of the patterns matched"
> 
> This scales well for adding more patterns. I usually use named groups in
> my Python regexs so the single "do something" block for matching any of
> the patterns works itself out.
> Trent




More information about the Python-list mailing list