Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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中使用正则表达式查找有效的电话号码_Python_Python 3.x_Regex_Re - Fatal编程技术网

在python中使用正则表达式查找有效的电话号码

在python中使用正则表达式查找有效的电话号码,python,python-3.x,regex,re,Python,Python 3.x,Regex,Re,输出:它不是一个电话号码 但是,当我传递输入时: phn1='412-1114-1234' if re.search("\d{3}-\d{3}-\d{4}",phn1): print('It is a phone number') else: print('It is not a phone number') 输出:它是一个电话号码 第二个电话号码输出错误的代码中有什么错误?它包含123-111-1234除第一个数字以外的所有内容。将正则表达式更改为:^\d{3}-\d{3}-\d

输出:它不是一个电话号码

但是,当我传递输入时:

phn1='412-1114-1234'

if re.search("\d{3}-\d{3}-\d{4}",phn1):
  print('It is a phone number')

else:
  print('It is not a phone number')
输出:它是一个电话号码


第二个电话号码输出错误的代码中有什么错误?

它包含123-111-1234除第一个数字以外的所有内容。将正则表达式更改为:^\d{3}-\d{3}-\d{4}$,以确保它只匹配整个输入。

it 123-111-1234除了第一个数字之外的所有内容。将正则表达式更改为:^\d{3}-\d{3}-\d{4}$以确保它只匹配整个输入。

这是搜索方法的行为,请尝试匹配。当您尝试搜索时,它会从123-111-1234此零件中找到匹配项。有关详细信息:

这是搜索方法的行为,请尝试匹配。当您尝试搜索时,它会从123-111-1234此零件中找到匹配项。有关更多信息:

您还需要包括一个结束标记$以避免匹配123-123-1234xyz例如,您还需要包括一个结束标记$以避免匹配123-123-1234xyz例如,设置边界\b\d{3}-\d{3}-\d{4}b\如果完整输入不应匹配或设置边界\b\d{3}-\d{3}-\d{4}b\如果完整的输入不应匹配
phn1='4123-111-1234'