1. 程式人生 > >Python-AttributeError: 'NoneType' object has no attribute 'groups'

Python-AttributeError: 'NoneType' object has no attribute 'groups'

原始碼

原始碼如下,報錯:

>>>import re
>>>re_telephone = re.compile(r'^(\d{3})-(\d{3,8})$')
>>>re_telephone.match('010-12345').groups()
>>>('010', '12345')
>>>re_telephone.match('01-12').groups()
Traceback (most recent call last):
 File "<input>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'

原因

如果沒有匹配到任何group,那麼就會返回這個錯誤。並不是語句寫錯了。