Python中的re.match(),re.search()和re.findall()方法有什么区别?
re.match(),re.search()和re.findall()是Python模块re的方法。
re.match()方法
如果re.match()方法出现在字符串的开头,则找到匹配项。例如,调用match()字符串“TPnhooo.comTP”并寻找模式“TP”将匹配。
示例
import re result = re.match(r'TP', 'TP nhooo.com TP') print result.group(0)
输出结果
TP
re.search()方法
re.search()方法与re.match()类似,但它并不限制我们仅在字符串的开头查找匹配项。
示例
import re result = re.search(r'Tutorials', 'TP nhooo.com TP') print result.group(0)
输出结果
Tutorials
re.findall()方法
re.findall()有助于获取所有匹配模式的列表。它从给定字符串的开头或结尾搜索。如果我们使用findall方法在给定的字符串中搜索模式,它将返回该模式的所有匹配项。在搜索模式时,建议始终使用re.findall(),两者的工作方式都类似于re.search()和re.match()。
示例
import re result = re.search(r'TP', 'TP nhooo.com TP') print result.group()
输出结果
TP
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便