I stumbled upon the following code:
import re
regex_compiled = re.compile('\d{2}-\d{3,5}')
res = re.search(regex_compiled, '12-9876')
I was under impression that re.search attempts to compile the first parameter which is already compiled so it should error or regex_compiled.__repr__() or regex_compiled.__str__() should be called just before a repeated attempt to compile it!
Just to be sure I compared it with regex_compiled.search(...:
>>> from timeit import timeit
>>> timeit("import re; regex_compiled = re.compile('\d{2}-\d{3,5}'); res = re.search(regex_compiled, '12-9876')")
1.3797054840251803
>>> timeit("import re; regex_compiled = re.compile('\d{2}-\d{3,5}'); res = regex_compiled.search('12-9876')")
0.7649686150252819
>>>
I am very puzzled from where so substantial difference comes from given that debugging into re.search (in both CPython v. 2 and v. 3) shows that the compiled pattern is reused! I hope someone can help shed some light on this.
Execution environment: Ubuntu 16.04, 64b