作者:LAMP小白 点击:2448 发布日期:2013-10-22 23:24:01 返回列表
search函数取模式和要扫描的文本作为输入,如果找到会返回一个match对象,如果没有找到则会返回None
编译表达式会更为高效,complie()函数会把一个表达式字符串转换为一个regexObject
模块级函数会维护已编译表达式的缓存,不过这个缓存的大小是有限制的,直接使用已编译的表达式可以避免缓存查找开销,通过在加载模块是预编译表达式,可以把编译工作转到程序开始,而不是在响应的时候即时编译
#!/usr/bin/python #coding:utf8 import re reg = r'this' text = 'this is a dog' r = re.search(reg, text) s = r.start() e = r.end() print "found 'this' in %snstart=%s and end=%s" % (text, s, e) regs = [ re.compile(p) for p in ['this', 'that'] ] text = 'this is a dog that is a mio' print 'text is %r' % text for reg in regs: print 'seeking %s ==>' % reg.pattern if reg.search(text): print 'seeked' else: print 'not found'
上一篇:python格式化段落 下一篇:快递查询API