【发布时间】:2018-06-15 07:49:59
【问题描述】:
在 Python 3.6 中,与 re.finditer 相比,为什么 re.findall 在以下示例中返回不同的项目?
text = "He was carefully disguised but captured quickly by 10 caps."
print(re.findall(r"ca(p)", text))
for i in re.finditer(r"ca(p)", text):
print(i)
findall 返回['p', 'p'],而 finditer 返回两个“cap”。只有当我使用括号时才会发生!
【问题讨论】:
-
您是指
(p)处的括号吗?你知道这些是捕获组吗?阅读有关这两种方法的文档,了解它们如何处理捕获组。
标签: python regex python-3.x findall