簡體   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