The use of python regular expressions

  • 2020-06-03 07:17:08
  • OfStack

python regularization is supported by the re module

Three matching functions

match: matches the regular expression only from the beginning of the string, returns matchobject if the match is successful, otherwise returns none;

re. match(pattern, string, flags=0) ##flags flag that controls how regular expressions are matched, such as case sensitivity, multi-line matching, and so on.

search: Try to match all strings of the string with the regular expression. If none of the strings matches successfully, return none, otherwise return matchobject. (re.search is equivalent to the default behavior in perl)

The findall method returns all 1 list matches the given expression;

use


mypatten = re.compile(" The rules ") ## Define matching rules 
myresult = mypatten.match(" string ") ## Matching results 

if myresult:

print myresult. group()## ## bracket can be filled with Numbers or groups (? P < name > Regular expression)#name is a valid identifier

search match1 sample


mypatten = re.compile(" The rules ") ## Define matching rules 
myresult = mypatten.findall(" string ") ## It returns a list   If there's a group in there that returns a 2 D list 

if myresult:


print myresult.group()

Related articles: