Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用RE验证python中的电话号码_Python 3.x - Fatal编程技术网

Python 3.x 使用RE验证python中的电话号码

Python 3.x 使用RE验证python中的电话号码,python-3.x,Python 3.x,我正在尝试使用以下语法,使用正则表达式验证电话号码。数字必须以9开头,其后有9位数字。你能告诉我这段代码可能有什么错误吗 import re phn = "9123456789" res = re.findall("(9)?[0-9]{9}", phn) print (res) O/p: ['9'] ?表示“可选”。如果您的号码必须以9开头,则不需要?: re.findall("9[0-9]{9}", phn) 或者更好: re.findall(r"9\d{9}", phn) 这是你

我正在尝试使用以下语法,使用正则表达式验证电话号码。数字必须以9开头,其后有9位数字。你能告诉我这段代码可能有什么错误吗

import re
phn = "9123456789"

res = re.findall("(9)?[0-9]{9}", phn)

print (res)

O/p:
['9']
表示“可选”。如果您的号码必须以9开头,则不需要

re.findall("9[0-9]{9}", phn)
或者更好:

re.findall(r"9\d{9}", phn)
这是你可能喜欢的:它使正则表达式变得简单

这是一个带有
9[0-9]{9}
的网站摘录:

可以使用python库PhoneNumber验证commonregex提取的电话号码:

import import commonregex
import phonenumbers

phone_list = commonregex.phone.findall(input_string)
for phone in phone_list:
    phonenumbers.is_valid_number(phonenumbers.parse(phone, None))

我希望它能帮助你。

明白了。为快速帮助而工作。@ BHARGAVMG请考虑通过点击它旁边的复选标记来接受答案。这同样适用于你之前的问题,因为你似乎也没有接受任何答案。