如果在函数内部编译正则表达式,并且该函数被多次调用,Python是否每次重新编译正则表达式,还是Python缓存编译后的正则表达式(假设正则表达式不会不会改变)?在多次调用函数内编译正则表达式
例如:
def contains_text_of_interest(line):
r = re.compile(r"foo\dbar\d")
return r.match(line)
def parse_file(fname):
for line in open(fname):
if contains_text_of_interest(line):
# Do something interesting
你是对的,请参阅[re.py中的_compile函数](https://github.com/python/cpython/blob/master/Lib/re.py#L278) – pevik 2016-03-30 08:33:49