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]
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短