Today I tried to cache re.compile to speed up my testing. Something got weird:
I used three cached re.compile:
re_cache1=re.compile(regexp_string1)
re_cache2=re.compile(regexp_string2)
re_cache3=re.compile(regexp_string3)
def mytest():
m = re_cache1.split(data)
if m:
for item in m:
m2 = re_cache2.search(data2)
if m2:
m3 = re_cache3.search(data3)
The result is not expected, m2 is None. But if I change a little bit, I will get m3’s match.
m2 = re.search(regexp_string2, data2)
Anybody helps me out? Thanks very much~
Keep in mind: the re module caches internally anyway. The difference between:
and
is one dictionary lookup. It’s usually not worth the extra bookkeeping to cache your own compiled regexes.