繁体   English   中英

Python Regex没有返回电话号码

[英]Python Regex not returning phone numbers

给出以下代码:

import re

file_object = open("all-OANC.txt", "r")
file_text = file_object.read()

pattern = "(\+?1-)?(\()?[0-9]{3}(\))?(-|.)[0-9]{3}(-|.)[0-9]{4}"

for match in re.findall(pattern, file_text):
    print match

我得到像这样延伸的输出:

('', '', '', '-', '-')
('', '', '', '-', '-')
('', '', '', '-', '-')
('', '', '', '-', '-')
('', '', '', '-', '-')
('', '', '', '-', '-')
('', '', '', '-', '-')
('', '', '', '-', '-')
('', '', '', '-', '-')

我正在尝试查找电话号码,我百分之百确定文件中有数字。 例如,当我在在线小程序中搜索数字时,使用相同的表达式,我得到匹配。

这是一个片段,其中表达式在python之外找到:

“Slate on Paper”,我们特别格式化的Slate打印版本,将于周五中午左右通过电子邮件发送给读者。 它也可以从我们的网站下载。 这些服务是免费的。 可以将“Slate on Paper”的实际纸质版本邮寄给您(致电800-555-4995),但这需要花钱,可能需要几天才能到达。“

我想要输出,至少可以识别数字的存在

这是您正在显示的捕获组。 显示整场比赛:

text = '''"Slate on Paper," our specially formatted print-out version of Slate, is e-mailed to readers Friday around midday. It also can be downloaded from our site. Those services are free. An actual paper edition of "Slate on Paper" can be mailed to you (call 800-555-4995), but that costs money and can take a few days to arrive."'''

pattern = "(\+?1-)?(\()?[0-9]{3}(\))?(-|.)[0-9]{3}(-|.)[0-9]{4}"    

for match in re.finditer(pattern,text):
    print(match.group())

输出:

800-555-4995

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM