简单的正则表达式的应用练习,从文本中匹配电话号码和邮箱地址
# coding=gbk
#encoding:utf-8
# -*- coding:gb2312 -*-
import re
import pyperclip
phoneregex = re.compile(r'''( #电话号码的正则表达式
(\d{3}|\(\d{3}\))?
(\s|-|\.)?
(\d{3})
(\s|-|\.)
(\d{4})
(\s*(ext|x|ext.)\s*(\d{2,5}))?
)''',re.VERBOSE )
emailregex = re.compile(r'''( #邮箱地址的正则表达式
[a-zA-Z0-9._%+-]+
@
[a-zA-Z0-9.+-]+
(\.[a-zA-Z]{2,4})
)''',re.VERBOSE)
text = str(pyperclip.paste()) #从粘贴板获取文本,也可直接打开文本文件或其他方式
matches = []
for groups in phoneregex.findall(text):
phonenum = '-'.join([groups[1],groups[3],groups[5]]) #按照统一格式显示:xxx-xxx-xxxx
if groups[8] != '':
phonenum += ' x'+groups[8]
matches.append(phonenum)
for groups in emailregex.findall(text): #邮箱地址全部加入matches
matches.append(groups[0])
if len(matches)>0:
pyperclip.copy('\n'.join(matches))
print('Copied to cilpboard:')
print('\n'.join(matches))
else :
print('No phone numbers or email address found.')