import re
phoneNumberRegex = re.compile(r'\d{3}-\d{3}-\d{4}')
mo = phoneNumberRegex.search('My number is 415-55-4242.')
print('Phone number found: ' + mo.group(0))
這是我使用的方法試圖找到一些錯誤的代碼都導致這個錯誤是什麼。集團()在正則表達式模塊的正確語法
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print('Phone number found: ' + mo.group(0))
AttributeError: 'NoneType' object has no attribute 'group'
您搜索的結果是'None',換句話說:沒有發現任何東西。你必須處理這種情況,也許有一個'if'子句。 –
它不會找到任何東西 - 你需要一個捕獲組:'(\ d {3} - \ d {3} - \ d {4})' –
你是不是指'\ d {2,3}'? –