Detailed explanation of non-greedy and multi-line matching functions of regular expressions in Python

黄舟
Release: 2017-08-08 11:05:14
Original
2057 people have browsed it

This article mainly introduces the non-greedy and multi-line matching functions of Python regular expressions. It analyzes the implementation methods and related precautions of the non-greedy and multi-line matching functions of Python regular expressions in the form of examples. Friends who need it can Refer to the following

The example of this article describes the non-greedy and multi-line matching function of Python regular expressions. Share it with everyone for your reference, the details are as follows:

Some regular tips:

1 Non-greedy flag


>>> re.findall(r"a(\d+?)","a23b") # 非贪婪模式
    ['2']
>>> re.findall(r"a(\d+)","a23b")
    ['23']
Copy after login

Pay attention to comparing this situation:


>>> re.findall(r"a(\d+)b","a23b")
    ['23']
>>> re.findall(r"a(\d+?)b","a23b") #如果前后均有限定条件,则非匹配模式失效
    ['23']
Copy after login

2 If you want to match multiple lines, add the re.S and re.M flags

re.S: . will match newline characters. By default, commas will not match newline characters.


>>> re.findall(r"a(\d+)b.+a(\d+)b","a23b\na34b")
    []
>>> re.findall(r"a(\d+)b.+a(\d+)b","a23b\na34b",re.S)
    [('23','34')]
>>>
Copy after login

re.M: The ^$ mark will match every line. By default, ^ will only match the first line that conforms to the regular pattern; by default, $ will only match the last line that conforms to the regular pattern


>>> re.findall(r"^a(\d+)b","a23b\na34b")
    ['23']
>>> re.findall(r"^a(\d+)b","a23b\na34b",re.M)
    ['23','34']
Copy after login

However, if there is no ^ sign,


>>> re.findall(r"a(\d+)b","a23b\na34b")
    ['23','43']
Copy after login

is visible and there is no need to re.M


import re
n='''12 drummers drumming,
11 pipers piping, 10 lords a-leaping'''
p=re.compile('^\d+')
p_multi=re.compile('^\d+',re.MULTILINE) #设置 MULTILINE 标志
print re.findall(p,n) #['12']
print re.findall(p_multi,n) # ['12', '11']
Copy after login


import re
a = 'a23b'
print re.findall('a(\d+?)',a) #['2']
print re.findall('a(\d+)',a) #['23']
print re.findall(r'a(\d+)b',a) #['23']
print re.findall(r'a(\d+?)b',a) # ['23']
Copy after login


b='a23b\na34b'
''' . 匹配非换行符的任意一个字符'''
print re.findall(r'a(\d+)b.+a(\d+)b',b) #[]
print re.findall(r'a(\d+)b',b,re.M) # ['23', '34']
print re.findall(r'^a(\d+)b',b,re.M) # ['23', '34']
print re.findall(r'a(\d+)b',b) #['23','34'] 可以匹配多行
print re.findall(r'^a(\d+)b',b) # ['23'] 默认^只会匹配符合正则的第一行
print re.findall(r'a(\d+)b$',b) # ['34'] 默认$只会匹配符合正则的末行
print re.findall(r'a(\d+)b',b,re.M) #['23', '34']
print re.findall(r'a(\d+)b.?',b,re.M) # ['23', '34'] 表达式中的'.'匹配除换行符以外的字符,'?'匹配前一个字符0次或1次
print re.findall(r"a(\d+)b", "a23b\na34b") # ['23', '34']
Copy after login

Note: In Python3.4, print is a function and requires parentheses

The above is the detailed content of Detailed explanation of non-greedy and multi-line matching functions 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