Python正则之预编译表达式re.compile()
来源:互联网 发布:淘宝直通车行业点击率 编辑:程序博客网 时间:2024/06/05 18:59
对于程序频繁使用的表达式,编译这些表达式会更有效。compile()函数会把一个表达式字符串转化成为一个RegexObject。
下面这个例子出自《Python标准库》:
import re# Precompile the patternsregexes = [re.compile(p) for p in ['this', 'that'] ]text = "Does this text match the pattern?"print 'Text: %r\n' % repr(text)for regex in regexes: print 'Seeking "%s"->' % regex.pattern, if regex.search(text): print 'match!' else: print 'no match'
输出:
Text: "'Does this text match the pattern?'"
Seeking "this"-> match!
Seeking "that"-> no match
模块级函数会维护已编译表达式的一个缓存。不过,这个缓存的大小是有限的,直接使用已编译的表达式可以避免缓存查找开销。使用已编译表达式的另一个好处是,通过在加载模块时预编译所有表达式,可以把编译工作转到应用开始时,而不是当程序响应一个用户动作时才进行编译。
1 0
- Python正则之预编译表达式re.compile()
- python正则表达式 re (二)compile
- Python正则表达式学习(3)——re.compile()
- Python中正则表达式re模块-compile()和findall()
- Python 正则re模块之compile()和findall()详解
- python re 正则表达式之二
- python学习之2 正则表达式re
- Python之re(正则表达式)模块小结
- python 正则表达式 之re.findall
- Python 之 re模块 正则表达式
- 20161003 Python笔记之正则表达式 re
- Python基础学习之re正则表达式
- python模块之re正则表达式
- python模块之re正则表达式
- python爬虫之re正则表达式库
- python系列之re模块(正则表达式)
- Python 正则表达式 re
- python-re【正则表达式】
- [Hibernate系列—] 2. 创建SessionFactory 与 Session
- 推理题 2014 TCO Round 2B 500 SumAndProductPuzzle
- 设计模式之迭代器模式
- 自己对集合知识几点总结
- Lucene 4.6.1 java.lang.IllegalStateException: TokenStream contract violation
- Python正则之预编译表达式re.compile()
- 【点击模型学习笔记】A survey on click modeling in web search_Lianghao Li_ppt
- C语言 运算符 优先级 和 口诀
- Tomcat端口号设置
- Zen Coding:css,html缩写替换大观---让你的html,css飞起来
- Effect of Switchovers, Failovers, and Control File Creation on Backups
- 一、排序_练习_1023_2
- 个人网站搬迁到CSDN
- 【音乐铃声定时播放器】红苹果MP3音乐铃声定时播放器