idiom for RE matching

Gordon Airporte JHoover at fbi.gov
Thu Jul 19 06:52:48 CEST 2007


I have some code which relies on running each line of a file through a 
large number of regexes which may or may not apply. For each pattern I 
want to match I've been writing

gotit = mypattern.findall(line)
if gotit:
	gotit = gotit[0]
	...do whatever else...

This seems kind of clunky. Is there a prettier way to handle this?
I've also been assuming that using the re functions that create match 
objects is slower/heavier than dealing with the simple list returned by 
findall(). I've profiled it and these matches are the biggest part of 
the running time of the program, so I really would rather not use 
anything slower.



More information about the Python-list mailing list