你可以看到它建立了 3 场比赛。左侧显示 Match 的索引和 Group 1 的索引。我想在 Python 中获取第 1 组的索引,我该怎么做?下面的图片显示了 Python 的返回值:
慕婉清6462132
浏览 327回答 2
2回答
慕雪6442864
您需要传递一个参数来i.span()指定要为其查找跨度的组(否则,它只是默认为整个匹配)。像这样:import res = 'aaadaa'matches = re.finditer(r'(?<=(aa))', s)for i in matches: print(i.span(1)) # This will work since you only have one capturing group, but if you have more than one you may have to make separate calls to .span()