python贪婪匹配中的错误

2024-04-25 20:38:16 发布

您现在位置:Python中文网/ 问答频道 /正文

import re
greedyHaRegex = re.compile(r'(Ha){3, 5}')
mo1 = greedyHaRegex.search('HaHaHaHaHa')
mo1.group()
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    mo1.group()
AttributeError: 'NoneType' object has no attribute 'group'

我不明白为什么会发生这个错误


Tags: importremostsearchgroupcallfilelast
1条回答
网友
1楼 · 发布于 2024-04-25 20:38:16

您的正则表达式不匹配,因为量词{3, 5}中有多余的空格,因此search()返回None删除空格,它将匹配。 例如使用regex101来调试这样的东西

相关问题 更多 >