This article mainly introduces the use of python regular expressions. Friends who need it can refer to it
Python's regular expressions are supported by the re module
3 matching Function
match: only matches the regular expression from the beginning of the string. If the match is successful, it returns matchobject, 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 the default behavior in perl )
findall method returns a list of all matching expressions;
Use
mypatten = re.compile("规则") ##定义匹配的规则 myresult = mypatten.match("字符串") ##匹配结果
if myresult:
print myresult.group()##You can fill in the brackets with numbers or name the group (?P
Search is the same as match
mypatten = re.compile("规则") ##定义匹配的规则 myresult = mypatten.findall("字符串") ##返回的是个列表 如果里面有分组返回的是个二维列表
if myresult:
print myresult.group()
The above is the detailed content of Application explanation of regular expressions in python. For more information, please follow other related articles on the PHP Chinese website!