Python is a cool language, because you can do a lot of things with very little code in a very short period of time, plus regular expressions to better reflect the results, the following article is mainly about Python in the pre-compiled regular expressions to improve the efficiency of the relevant data, You need a friend to refer to below.
Objective
In the regular expression module of RE, the regular expression can be accessed through a module, but if you use regular expressions repeatedly, it is better to use the compile function to compile the regular expression into object Regexobject, which will greatly improve the efficiency of the search. Because access is based on non-compiled methods, it is done using a small piece of buffering in the module.
As in the following example:
Import re # precompile the Patterns regexes = [ re.compile (P) for P in [' This ', ' that ']] text = ' Http://blog. Csdn.net/caimouse is great blog, the Is my blog. ' Print (' text: {!r}\n '. Format (text)) for the regex in Regexes: print (' Seeking ' {} '. Format (Regex.pattern), end= ") If Regex.search (text): print (' match! ') else: print (' no match ')
The resulting output is as follows:
text: ' Http://blog.csdn.net/caimouse is great blog, the Is my blog. ' Seeking ' this ' match ! Seeking "that", no match