Lessons · Regex · the pattern cache, and when it stops helping
Compiling, and what it is really for
The module-level calls look the compiled pattern up in a cache, so the usual advice to compile is about clarity, not about a large hidden cost.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Knowing this stops you optimising the wrong thing. The case that genuinely needs compiling is the one where the pattern is built per row and the cache never hits.
How to think about it
Compile for a name and a place to put a comment. Compile for real when the patterns are made from data rather than written down.
Worked example
re.compile(r'\d+').findall('a1b22')A pattern object, with the methods on it.re.findall(r'\d+', 'a1b22')The same work, through the cache.
re.compile(r'\d+').patternAnd the object remembers what it was made from.
Your turn
Build the pattern once and use it.
re.(r'\d+').findall('a1b22')Test a pattern against real text
The trap
The engine is identical either way. If a regex is your bottleneck, the pattern is the thing to change, not where it was built.