Python实践练习:电话号码和 E-mail 地址提取程序

时间:2021-06-10 05:04:21

题目:

假设你有一个无聊的任务,要在一篇长的网页或文章中,找出所有电话号码和邮件地址。如果手动翻页,可能需要查找很长时间。如果有一个程序,可以在剪贴板的文本中查找电话号码和 E-mail 地址,那你就只要按一下 Ctrl-A 选择所有文本,按下 Ctrl-C 将它复制到剪贴板,然后运行你的程序。它会用找到的电话号码和 E-mail地址,替换掉剪贴板中的文本。

测试文本

Skip to main content
Home
Search form Search GO!
Topics
Arduino
Art & Design
General Computing
Hacking & Computer Security
Hardware / DIY
JavaScript
Kids
LEGO®
LEGO® MINDSTORMS®
Linux & BSD
Skip to main content
Home
Search form Search GO!
Catalog
Media
Write for Us
About Us
Topics
Arduino
Art & Design
General Computing
Hacking & Computer Security
Hardware / DIY
JavaScript
Kids
LEGO®
LEGO® MINDSTORMS®
Linux & BSD
Manga
Minecraft
Programming
Python
Science & Math
Scratch
System Administration
Early Access
Gift Certificates
Free ebook edition with every print book purchased from nostarch.com!
Shopping cart
3 Items Total: $53.48
View cart Checkout
Contact Us No Starch Press, Inc.
245 8th Street
San Francisco, CA 94103 USA
Phone: 800.420.7240 or +1 415.863.9900 (9 a.m. to 5 p.m., M-F, PST)
Fax: +1 415.863.9950 Reach Us by Email
General inquiries: info@nostarch.com
Media requests: media@nostarch.com
Academic requests: academic@nostarch.com (Please see this page for academic review requests)
Help with your order: info@nostarch.com
Reach Us on Social Media
Twitter
Facebook
Navigation
My account
Log out
Manage your subscription preferences. About Us | ★ Jobs! ★ | Sales and Distribution | Rights | Media | Academic Requests | Conferences | Order FAQ | Contact Us | Write for Us | Privacy
Copyright 2018 No Starch Press, Inc

运行后结果

Copied to clipboard:
800-420-7240
415-863-9900
415-863-9950
info@nostarch.com
media@nostarch.com
academic@nostarch.com
info@nostarch.com
Hit any key to close this window...

思路

当你开始接手一个新项目时,很容易想要直接开始写代码。但更多的时候,最好是后退一步,考虑更大的图景。我建议先草拟高层次的计划,弄清楚程序需要做什么。暂时不要思考真正的代码,稍后再来考虑。

1.创建电话的正则表达式和创建email的正则表达式

2.匹配剪切板的文本

3.把处理好的文本复制到剪切板

现在开始写程序

#! python3
# phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard. import re, pyperclip
# 创建电话的正则表达式
phoneRegex = re.compile(r'''(
(\d{3}|\(d{3}\))? # 区号可选,444或(444)
(\s|-|\.)? # 分隔符:字符或-或. 可选
(\d{3}) # 三个数字
(\s|-|\.)? # 分隔符:字符或-或. 可选
(\d{4}) # 四个数字
)''',re.VERBOSE) # 创建email的正则表达式
emailRegex = re.compile(r'''(
[a-zA-Z0-9._%+-]+ # username
@
[a-zA-Z0-9.-]+ # domail name
(\.[a-zA-Z]{2,4}) # dot-something
)''',re.VERBOSE) # 匹配剪切板的文本
text = str(pyperclip.paste())
matches = []
for groups in phoneRegex.findall(text):
phoneNum = '-'.join([groups[1], groups[3], groups[6]])
matches.append(phoneNum)
for groups in emailRegex.findall(text):
matches.append(groups[0]) # 把处理好的文本复制到剪切板
if len(matches) > 0:
pyperclip.copy('\n'.join(matches))
print('Copied to clipboard:')
print('\n'.join(matches))
else:
print('No phone numbers or email addresses found.')

分析代码

re.VERBOSE是让正则表达式中可以忽略注释和空白符的一个参数。verbose表示冗杂的意思,就是可以让你添些注释,对正则更可读。

正则表达式详见:Python正则

另一个坑就是groups了,原来我没有理解groups与group的区别

group()是截取分组的意思,例子:

import re
a = "123abc456"
print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(0) #123abc456,返回整体
print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(1) #123
print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(2) #abc
print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(3) #456

groups() 返回一个包含所有小组字符串的元组,从 1 到 所含的小组号。

代码中phoneNum = '-'.join([groups[1], groups[3], groups[6]])中的groups是一个变量,别看错了。