I`ve發現這似乎是很奇怪的,我的例子:
import re
for test_string in ['955-1212', 'ILL-EGAL']:
if re.match(r'^\d{3}-\d{4}$', test_string):
print test_string, 'is a valid US local phone number'
else:
print test_string, 'rejected'
輸出爲:955-1212是有效的美國本地電話號碼 ILL-EGAL拒絕
究竟什麼是 「^ \ d {3} - \ d {4} $」,因爲當我改變了一些數字,它的輸出改變: 955-1212拒絕 ILL-EGAL拒絕
http://docs.python.org/library/re.html – jdi 2012-07-17 05:13:52
當我運行此我得到「955-1212是有效的美國本地電話號碼 ILL-EGAL拒絕」 - 但只有在我縮進線從「如果」起... – Sean 2012-07-17 05:18:17