给出以下代码:
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打印版本,通过电子邮件发送给读者 星期五中午左右。它也可以从我们这里下载 现场。这些服务是免费的。 “纸上石板”的实际纸质版 可以邮寄给你(电话800-555-4995),但这需要花钱,可以花一点钱 几天到达。“
我希望输出至少能识别出数字
答案 0 :(得分:3)
它是您正在显示的捕获组。显示整场比赛:
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