matching multiple regexs to a single line...

Alexander Sendzimir lists at battleface.com
Tue Nov 19 20:27:33 EST 2002


Come to think of it, you're right!

The regex ids are a hold over. Whether using a list or a
dictionary, regex and handler are sufficient.

Why re and no sre?

Thanks, John.


Alex




On Tue, 19 Nov 2002 18:16:02 +0000, jdhunte wrote:

> 
> Wouldn't it be a simpler and more readily maintainable design drop the
> regex_ids and use a list of tuples where the first element is a rgx
> and the second element is an action?  In your version you have to
> maintain a dictionary and a list, as well as (somewhat superfluous)
> regex ids.
> 
> import re                                         # you do want the re wrapper, not sre
> lines = map(str,range(3))                         # some dummy lines for testing
> handler1 = handler2 = handler3 = lambda x: None   # some very dumb handlers
> 
> regexs = (
>     ( re.compile( r'regex1' ), handler1 ),
>     ( re.compile( r'regex2' ), handler2 ),
>     ( re.compile( r'regex3' ), handler3 ),
>     )
> 
> for line in lines :
>    for (rgx, action) in regexs:
>       m = rgx.match( line )
>       if m: action(m)
>         
> 
> John Hunter




More information about the Python-list mailing list