Python is a cool language, because you can do a lot of things with very little code in a short time, and regular expressions can better reflect its effect. The following article mainly introduces it to you. I have related information about improving efficiency by pre-compiling regular expressions in python. Friends in need can refer to it.
Preface
In the re regular expression module, you can access regular expressions through the module, but if it is repeated multiple times To use regular expressions properly, it is best to use the compile function to compile the regular expression into an object RegexObject. This will greatly improve the efficiency of the search, because when accessed in a non-compiled way, a small buffer in the module is used. .
For example:
##
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')
Text: 'http://blog.csdn.net/caimouse is great blog, this is my blog.' Seeking "this" -> match! Seeking "that" -> no match
The above is the detailed content of Detailed explanation of how to improve efficiency of compiling regular expressions in python. For more information, please follow other related articles on the PHP Chinese website!