matching multiple regexs to a single line...
jdhunter at ace.bsd.uchicago.edu
jdhunter at ace.bsd.uchicago.edu
Tue Nov 19 19:16:02 EST 2002
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