Home  >  Article  >  Backend Development  >  Application explanation of regular expressions in python

Application explanation of regular expressions in python

巴扎黑
巴扎黑Original
2017-08-08 16:01:041128browse

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 (?P8a11bc632ea32a57b3e3693c7987c420regular expression)#name is a legal identifier

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn