[英]Regex find this specific phone numbers from string Python
i'm trying to create a python script that extract phone numbers from string using regex .我正在尝试创建一个 python 脚本,该脚本使用 regex 从字符串中提取电话号码。 My actual code are able to retrieve 7/11 formats.我的实际代码能够检索 7/11 格式。 Here are all the 11 numbers formats:以下是所有 11 种数字格式:
000-000-0000
000 000 0000
000.000.0000
(000)000-0000
(000)000 0000
(000)000.0000
(000) 000-0000
(000) 000 0000
(000) 000.0000
0000000000
(000)0000000
import re
Test = '000-000-0000 number 1 000 000 0000 number 2 000.000.0000 nbr3 (000)000-0000 nbr4 (000)000 0000 nbr5 (000)000.0000 nbr6 (000) 000-0000 nbr7 (000) 000 0000 nbr8 (000) 000.0000 nbr9 000-0000 nbr10 000 0000 nbr11 000.0000 nbr12 0000000 nbr13 0000000000 nbr14 (000)0000000 nbr'
phonetype1 = re.findall(r'(\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4} | \(\d{3}\)\s *\d{3}[-\.\s]??\d{4} )', Test)
print(phonetype1)
print(len(phonetype1))
The output is: output 是:
['000-000-0000 ', '000 000 0000 ', '000.000.0000 ', ' (000) 000-0000 ', ' (000) 000 0000 ', ' (000) 000.0000 ', '0000000000 ']
7
The formats (4/11) that i'm trying to retrieve are:我试图检索的格式(4/11)是:
(000)000-0000
(000)000 0000
(000)000.0000
(000)0000000
Use this pattern使用此模式
\(\d{3}\)\d{3}([-.\s])?\d{4}
Try test cases Regex Demo尝试测试用例Regex Demo
The selected matches选定的匹配项
(000)000-0000
(000)000 0000
(000)000.0000
(000)0000000
This regex is working with all formats:此正则表达式适用于所有格式:
^((\d{3}-\d{3}-\d{4})|(\d{3}\ \d{3}\ \d{4})|(\d{3}\.\d{3}\.\d{4})|(\(\d{3}\)\ ?\d{3}[- .]?\d{4})|\d{10})$
or you can use it to get matches for phone numbers not beginning and ending in same line with this pattern:或者您可以使用它来获取与此模式不以同一行开头和结尾的电话号码的匹配项:
(\d{3}-\d{3}-\d{4})|(\d{3}\ \d{3}\ \d{4})|(\d{3}\.\d{3}\.\d{4})|(\(\d{3}\)\ ?\d{3}[- .]?\d{4})|\d{10}
You can check the demo , this approach is very restrictive, and matches only the patterns you mentionned.您可以查看演示,这种方法非常严格,并且仅匹配您提到的模式。
You could write the following.您可以编写以下内容。
import re
s = '000.000.0000 (000)000-0000 (000)000 0000 (000)000.0000 (000) 000-0000 (000) 000 0000 (000) 000.0000 0000000000 (000)0000000'
print re.findall(r'\(\d{3}\)\d{3}(?:-| |.|)\d{4}(?!\d)', s)
#=> ['(000)000-0000', '(000)000 0000', '(000)000.0000', '(000)0000000']
Python's regex engine performs the following operations. Python 的正则表达式引擎执行以下操作。
\(\d{3}\) match 3 digits in parentheses
\d{3} match 3 digits
(?:-| |.|) match '-', ' ', '.' or '' (empty string)
\d{4} match 4 digits
(?!\d) next char cannot be a digit (negative lookahead)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.