一、贪婪模式简介
非贪婪模式(.*?) 即尽可能少的匹配字符:
.表示单个字符
* 表示{0,} 0到无穷多个字符
?表示{0,1} 0到1个字符
贪婪模式和非贪婪模式的对比
贪婪模式会尽可能多的匹配字符
import re
s = 'abcdecccccfg'
r1 = re.findall("ab.*c", s) #贪婪模式
print(r1) #['abcdeccccc']
r2 = re.findall("ab.*?c", s) #非贪婪模式
print(r2) #['abc']
二、贪婪模式的应用
import re
s = ""
with open("text.html", "r", encoding='utf-8')as f:
s = f.read()
print(s)
s = re.findall('<dd id="contents">(.*?)</dd>', s, re.S)[0] #想把列表变成字符串[0]
s = s.replace("<br />", "")
s = s.replace(" ", "")
print(s)
三、预定义字符组:[ ]
1、\d==[0-9]
import re
s = "<a href='asdf'>121423646</a>"
a = re.findall("(\d.*)", s) #贪婪模式
b = re.findall("(\d.*\d)", s) #贪婪模式
pr