Python使用`re.finditer`遍历比赛
示例
您可以re.finditer用来遍历字符串中的所有匹配项。与其他re.findall信息(例如,有关字符串(索引)中匹配位置的信息)相比,这可以为您提供:
import re
text = 'You can try to find an ant in this string'
pattern = 'an?\w' #查找带有或不带有后续单词字符的“一个”
for match in re.finditer(pattern, text):
#匹配开始索引(整数)
sStart = match.start()
#最终匹配索引(整数)
sEnd = match.end()
#完全匹配(字符串)
sGroup = match.group()
#打印比赛
print('Match "{}" found at: [{},{}]'.format(sGroup, sStart,sEnd))结果:
Match "an" found at: [5,7] Match "an" found at: [20,22] Match "ant" found at: [23,26]
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短