前言
在re的正則表達式模塊里,可以通過模塊的方式來訪問正則表達式,但是如果重復(fù)多次地使用正則表達式,最好是使用compile函數(shù)把正則表達式編譯成對象RegexObject,這樣會大大地提高搜索的效率,因為基于非編譯方式訪問時,是使用模塊里的一小塊緩沖來進行的。
如下面的例子:
import re # Precompile the patterns regexes = [ re.compile(p) for p in ['this', 'that'] ] text = 'http://blog.csdn.net/caimouse is great blog, this is my blog.' print('Text: {!r}/n'.format(text)) for regex in regexes: print('Seeking "{}" ->'.format(regex.pattern), end=' ') if regex.search(text): print('match!') else: print('no match') 結(jié)果輸出如下:
Text: 'http://blog.csdn.net/caimouse is great blog, this is my blog.'Seeking "this" -> match!Seeking "that" -> no match
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林站長站的支持。
新聞熱點
疑難解答