regular expression dictionary search

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Aug 20 21:32:44 CEST 2007


On 20 ago, 12:48, mkPyVS <mikemine... at hotmail.com> wrote:
> On Aug 20, 9:35 am, "Shawn Milochik" <Sh... at Milochik.com> wrote:
>
> As a side note unless you are searching large buffers it is possibly
> more costly to compile into a re object then do a match with it as
> opposed to let the match object perform a compile a function level
> itself- if you use the class option above I would recommend storing
> the re.compiled versions of your patterns in the dictionary
> (everything is an object!) rather than the string repr and issuing a
> compile.

The re module already does exactly that - compiled expressions are
cached in a dictionary (but this fact appears to be undocumented, so
one should not rely heavily on this).
Anyway, if you have the regular expression object, it's better to call
its match method directly instead of using the module match function.
Using re.match(compiled_regex,...) is the worst case. In the posted
original code, replace re.match(regex, aWord) with regex.match(aWord).

--
Gabriel Genellina




More information about the Python-list mailing list