Home > Article > Backend Development > How to use python regular expressions
Python's regular expression is supported by the re module.
Three matching functions
match: only matches the regular expression from the beginning of the string, and returns matchobject if the match is successful, otherwise it returns none;
re.match(pattern, string, flags=0) ##flags flag bit, used to control the matching method of regular expressions , such as: whether to be case-sensitive, multi-line matching, etc.
search: Try to match all the strings of the string with the regular expression. If all the strings do not match successfully, return none, otherwise return matchobject; (re.search is equivalent to perl Default behavior)
findall method returns a list of all matching expressions;
Use
mypatten = re.compile("Rule") ##Define matching rules
myresult = mypatten.match("String") ##Matching results
if myresult:
print myresult.group()##You can fill in the numbers in the brackets or name the group (? P
##search is the same as match
mypatten = re.compile("Rule") ##Define matching rules
myresult = mypatten. findall("string") ##Returns a list. If there are groups, it returns a two-dimensional list
##if myresult:myresult.group()
The above is the detailed content of How to use python regular expressions. For more information, please follow other related articles on the PHP Chinese website!