用于验证移动电话号码,如
(111)-111-1111
我不认为你需要像这样一个过于复杂和不正确的正则表达式
([(+*)]\d{3}[(+*)][a-]\d{3}[a-]\d{4})
即使你把起锚
^
和端锚
$
它将验证以下手机号码是否有效,如果正确的话,
)111(a111a1111
*111+-111-1111
Check this demo to see how it allows invalid mobile numbers
用于验证这样的移动电话号码
(111)-111-1111
,您可以使用以下正则表达式,
^\(\d{3}\)-\d{3}-\d{4}$
Demo for correctly validating mobile numbers
如果你想让我知道
(111)-111-1111
手机号码有效。
另外,对于验证文本,应该使用
match
功能而不是
findall
后者用于从文本中提取信息,前者用于匹配文本以获得有效性。
下面是一个示例python代码,它展示了如何验证移动电话号码,
import re
arr = ['(111)-111-1111','(((((111)-111-1111',')111(a111a1111','*111+-111-1111']
for s in arr:
if (re.match(r'^\(\d{3}\)-\d{3}-\d{4}$', s)):
print(s, ' --> is Valid mobile number')
else:
print(s, ' --> is Not Valid mobile number')
印刷品,
(111)-111-1111 --> is Valid mobile number
(((((111)-111-1111 --> is Not Valid mobile number
)111(a111a1111 --> is Not Valid mobile number
*111+-111-1111 --> is Not Valid mobile number