Application explanation of regular expressions in python

巴扎黑
Release: 2017-08-08 16:01:04
Original
1138 people have browsed it

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("字符串") ##匹配结果
Copy after login

if myresult:

 print myresult.group()##You can fill in the brackets with numbers or name the group (?Pregular expression)#name is a legal identifier

Search is the same as match


mypatten = re.compile("规则") ##定义匹配的规则
myresult = mypatten.findall("字符串") ##返回的是个列表 如果里面有分组返回的是个二维列表
Copy after login

if myresult:


print myresult.group()
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template