复习re
findall
list = re.findall(r"\d+","我电话号是:2195913,32958")
print(list) #匹配全部的符合条件的信息
输出['2195913', '32958']
迭代器
findliter
list1 = re.finditer(r"\d+","我电话号是:2195913,32958")
for i in list1:
print(i.group()) #从迭代器里拿数据用group
输出2195913
32958
search
只找一次
list1 = re.search(r"\d+","我电话号是:2195913,32958")
print(list1.group())
2195913
match
从头开始匹配
list1 = re.search(r"\d+","我电话号是:2195913,32958")
print(list1.group())
2195913
预加载正则表达式
obj = re.compile(r"\d+") #预加载
list = obj.finditer("我电话号是:2195913,32958")
for i in list:
print(i.group())
例子
ss = """ <a href="http://health.sohu.com/">健康</a>
<a href="http://history.sohu.com/">历史</a>
<a href="http://mil.sohu.com/">军事</a>
<a href="http://chihe.sohu.com/">美食</a>
<a href="http://cul.sohu.com/">文化</a>
"""
obj = re.compile(r"<a href=(?P<wa>.*?)</a>",re.S) #re.s 让.*? 能匹配换行符
list = obj.finditer(ss)
for i in list:
print(i.group("wa"))
输出:"http://health.sohu.com/">健康
"http://history.sohu.com/">历史
"http://mil.sohu.com/">军事
"http://chihe.sohu.com/">美食
"http://cul.sohu.com/">文化