首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Python的re.compile值得吗?

使用Python的re.compile值得吗?
EN

Stack Overflow用户
提问于 2009-01-16 21:31:58
回答 27查看 359.8K关注 0票数 565

在Python中使用编译正则表达式有什么好处吗?

代码语言:javascript
运行
复制
h = re.compile('hello')
h.match('hello world')

vs

代码语言:javascript
运行
复制
re.match('hello', 'hello world')
EN

Stack Overflow用户

发布于 2009-07-06 10:13:44

(几个月后)很容易在re.match周围添加自己的缓存,或者其他任何东西--

代码语言:javascript
运行
复制
""" 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() ->大小,命中,清除...

票数 1
EN
查看全部 27 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/452104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档