在Python中使用编译正则表达式有什么好处吗?
h = re.compile('hello')
h.match('hello world')
vs
re.match('hello', 'hello world')
发布于 2009-07-06 10:13:44
(几个月后)很容易在re.match周围添加自己的缓存,或者其他任何东西--
""" Re.py: Re.match = re.match + cache
efficiency: re.py does this already (but what's _MAXCACHE ?)
readability, inline / separate: matter of taste
"""
import re
cache = {}
_re_type = type( re.compile( "" ))
def match( pattern, str, *opt ):
""" Re.match = re.match + cache re.compile( pattern )
"""
if type(pattern) == _re_type:
cpat = pattern
elif pattern in cache:
cpat = cache[pattern]
else:
cpat = cache[pattern] = re.compile( pattern, *opt )
return cpat.match( str )
# def search ...
如果: cachehint( size= ),cacheinfo() ->大小,命中,清除...
https://stackoverflow.com/questions/452104
复制相似问题