Compiling regex inside function?

Duncan Booth duncan.booth at invalid.invalid
Mon Aug 3 19:47:05 CEST 2009


alex23 <wuwei23 at gmail.com> wrote:

> The docs say:
> The compiled versions of the most recent patterns passed to re.match
> (), re.search() or re.compile() are cached, so programs that use only
> a few regular expressions at a time needn’t worry about compiling
> regular expressions.
> 
> (But they don't say how few is 'only a few'...)

If you're interested then it isn't all that hard to open up re.py in an 
editor and see that the cache holds up to 100 items. Of course that's just 
an implementation detail so other versions might have a different size 
cache.

Also implementation dependant, but it might be worth knowing that when you 
hit that limit the cache is simply cleared: there's no gradual clearing of 
the oldest or least used entries.



More information about the Python-list mailing list