gpt4 book ai didi

python - 正则表达式匹配的数量

转载 作者:IT老高 更新时间:2023-10-28 20:23:06 24 4
gpt4 key购买 nike

我正在使用 re 模块中的 finditer 函数来匹配一些东西,并且一切正常。

现在我需要找出我有多少匹配项。是否可以不循环遍历迭代器两次? (一个找出计数然后真正的迭代)

一些代码:

imageMatches = re.finditer("<img src\=\"(?P<path>[-/\w\.]+)\"", response[2])
# <Here I need to get the number of matches>
for imageMatch in imageMatches:
doStuff

一切正常,我只需要在循环之前获取匹配数。

最佳答案

如果您知道需要所有匹配项,则可以使用 re.findall 函数。它将返回所有匹配项的列表。然后你可以对匹配的数量执行 len(result)

关于python - 正则表达式匹配的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3895646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com