简体   繁体   English

Python Regex没有返回电话号码

[英]Python Regex not returning phone numbers

Given the following code: 给出以下代码:

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

I get output that stretches like this: 我得到像这样延伸的输出:

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

I'm trying to find phone numbers, and I am one hundred percent sure there are numbers in the file. 我正在尝试查找电话号码,我百分之百确定文件中有数字。 When I search for numbers in an online applet for example, with the same expression, I get matches. 例如,当我在在线小程序中搜索数字时,使用相同的表达式,我得到匹配。

Here is a snippet where the expression is found outside of python: 这是一个片段,其中表达式在python之外找到:

"Slate on Paper," our specially formatted print-out version of Slate, is e-mailed to readers Friday around midday. “Slate on Paper”,我们特别格式化的Slate打印版本,将于周五中午左右通过电子邮件发送给读者。 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." 可以将“Slate on Paper”的实际纸质版本邮寄给您(致电800-555-4995),但这需要花钱,可能需要几天才能到达。“

I want output that at least recognizes the presence of a number 我想要输出,至少可以识别数字的存在

It's your capture groups that are being displayed. 这是您正在显示的捕获组。 Display the whole match: 显示整场比赛:

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())

Output: 输出:

800-555-4995

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

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