一致の数を指定する必要があります{m, n}
または{m}
が好き:VERBOSE正規表現を使用した作業
regexp = r'''
# matches phones, but not zipcodes. Use with VERBOSE regexps
^ # start of the string
\s*? # whitespaces, etc
\+? # + char (optional)
\s*? # whitespaces, etc
\(? # (char (optional)
([0-9]{3}) # 3 numbers
\)? #) char (optional)
( # group start
[\s-]? # whitespace, - char (optionals)
[0-9] # 1 number
){7} # matches exactly 7 numbers
\s*? # whitespaces, etc
$ # end of the string
'''
phones = ['(123) 456 7899', '(123)-456-7899', '+1234567899', '+123 456-7899',
'12-34567899', '+123456789'] # these 2 dont match
matches = [bool(re.match(regexp, num, re.VERBOSE)) for num in phones]
print(matches) # gives [True, True, True, True, False, False]
はあなたの偉大なデバッグ
最後の数値を3つ以上の整数にすることができます。 https://regex101.com/r/gV1qP8/1 '\ d {3、}'は本当に電話番号を必要としません。地元のコードを使用した米国の郵便番号はまだ一致します。 – chris85
@ chris85に加えて、各桁を個別にチェックしているので正規表現を整理する必要があります。 – albert