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'
我不明白为什么会发生这个错误
您的正则表达式不匹配,因为量词
{3, 5}
中有多余的空格,因此search()
返回None
删除空格,它将匹配。 例如使用regex101来调试这样的东西相关问题 更多 >
编程相关推荐