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]
热门推荐
2 训练狗狗成为
3 训练狗狗几岁
4 反应训练狗狗
5 听力训练狗狗
7 狗狗尿训练
8 拳击训练狗狗
10 狗狗训练卧室
11 一个月狗狗怎么训练
12 几个月训练狗狗大小便
13 牧羊犬训练狗狗
14 狗狗训练做
15 狗狗训练它
16 可以训练狗狗
17 训练狗狗给球
18 训练狗狗的技巧