2、Search
re.match 从字符串开头进行匹配,如果匹配规则不能对字符串开头进行匹配,则会匹配失败,返回None。因此,re.match 再对字符串中内容进行匹配时并不方便,反而更适合检测字符串是否符合特定的规则。
re.match 对字符串开头不匹配导致匹配失败的示例如下:
import re
text = "Python Hello world \n1234567 python is the best language!"
pattern = "Hello.*?(\d+)"
result = re.match(pattern,text, re.S)
print(result)
运行输出如下:
None
将re.match 换成 re.search 匹配成功的示例如下:
# *********************************************
# use of re.search
# *********************************************
import re
text = "Python Hello world \n1234567 python is the best language!"
pattern = "Hello.*?(\d+)"
result = re.search(pattern,text, re.S)
print(result)
运行输出结果如下:
<re.Match object; span=(7, 27), match='Hello world \n1234567'>
3、findall
re.search 方法,可以返回与表达式相匹配的第一个字符串,而如果需要获取相匹配的所有字符串,可以使用 findall 方法。使用方法如下:
import re
html = '''<div id="songs-list">
<h2 class="title">经典老歌</h2>
<p class="introduction">
经典老歌列表
</p>
<ul id="list" class="list-group">
<li data-view="2">一路上有你</li>
<li data-view="7">
<a href="/2.mp3" singer="任贤齐">沧海一声笑</a></li>
<li data-view="3" class="active">
<a href="/3.mp3" singer="齐秦">往事随风</a></li>
<li data-view="4"><a href="/4.mp3" singer="beyond">光辉岁月</a> </li>
</ul>
</div>'''
pattern = '<li.*?singer="(.*?)">(.*?)</a>'
results = re.findall(pattern,html,re.S)
print(results)
for result in results:
print(result[0],result[1], sep=":")
运行输出结果如下:
[('任贤齐', '沧海一声笑'), ('齐秦', '往事随风'), ('beyond', '光辉岁月')]
任贤齐:沧海一声笑
齐秦:往事随风
beyond:光辉岁月
findall 方法返回的内容为列表格式,每个元素都是元组类型,可以对匹配得到的所有内容进行输出。
4、sub
re.sub 方法可以将正则表达式匹配到的内容进行修改,可以实现对匹配内容的替换。使用方法如下:
import re
text = "Hello world \n1234567 python is the best language!"
pattern = '\d+'
result = re.sub(pattern, "I think", text, re.S)
print(result)
运行输出结果如下:
Hello world
I think python is the best language!
5、compile
re.compile 方法可以将字符串编译成正则表达式对象,在匹配中直接使用,使用方法如下:
import re
text = "Hello world \n1234567 python is the best language!"
pattern = re.compile('\n\d{7}')
result = re.sub(pattern, ", I think", text, re.S)
print(result)
运行输出结果如下:
Hello world , I think python is the best language!
后续公众号会发布系列教程,更多内容请关注公众号:程序猿学习日记