re.compile function in python re.compile function in python

re.compile function in python

 

Regular expressions are very powerful.

"Some people face a problem and think: 'I know, I can use regular expressions to solve this problem.' So now they have two problems" - Jamie Zawinski

At the same time regular expressions are hard to master.

The various rules of regular expressions will not be repeated here. The following describes how to apply regular expressions in python's re module

1. Use re.compile

An important function included in the re module is compile(pattern [, flags]), which creates a pattern object from a string containing regular expressions . More efficient matching can be achieved. When performing search, match and findall operations directly using the regular expression represented by the string, python converts the string to a regular expression object. After using compile to complete a conversion, there is no need to repeat the conversion every time the pattern is used. Of course, after using the re.compile() function for conversion, the calling method of re.search(pattern, string) is converted to the calling method of pattern.search(string).

Among them, in the latter calling method, pattern is the pattern object created by compile. as follows:

>>> import re
>>> some_text = 'a,b,,,,c d'
>>> reObj = re.compile('[, ]+')
>>> reObj.split(some_text)
['a', 'b', 'c', 'd']

 

2. Do not use re.compile

The compile function is not applicable before search, match and other operations, which will lead to repeated conversion of the pattern when the pattern is reused. Reduce matching speed. The calling method of this method is more intuitive. as follows:

>>> import re
>>> some_text = 'a,b,,,,c d'
>>> re.split('[, ]+',some_text)
['a', 'b', 'c', 'd']

Regular expressions are very powerful.

"Some people face a problem and think: 'I know, I can use regular expressions to solve this problem.' So now they have two problems" - Jamie Zawinski

At the same time regular expressions are hard to master.

The various rules of regular expressions will not be repeated here. The following describes how to apply regular expressions in python's re module

1. Use re.compile

An important function included in the re module is compile(pattern [, flags]), which creates a pattern object from a string containing regular expressions . More efficient matching can be achieved. When performing search, match and findall operations directly using the regular expression represented by the string, python converts the string to a regular expression object. After using compile to complete a conversion, there is no need to repeat the conversion every time the pattern is used. Of course, after using the re.compile() function for conversion, the calling method of re.search(pattern, string) is converted to the calling method of pattern.search(string).

Among them, in the latter calling method, pattern is the pattern object created by compile. as follows:

>>> import re
>>> some_text = 'a,b,,,,c d'
>>> reObj = re.compile('[, ]+')
>>> reObj.split(some_text)
['a', 'b', 'c', 'd']

 

2. Do not use re.compile

The compile function is not applicable before search, match and other operations, which will lead to repeated conversion of the pattern when the pattern is reused. Reduce matching speed. The calling method of this method is more intuitive. as follows:

>>> import re
>>> some_text = 'a,b,,,,c d'
>>> re.split('[, ]+',some_text)
['a', 'b', 'c', 'd']

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325294723&siteId=291194637