简单的正则表达式的应用练习,从文本中匹配电话号码和邮箱地址
# 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.')
本文介绍了一种使用正则表达式从文本中提取电话号码和邮箱地址的方法。通过Python的re模块和pyperclip库,文章展示了如何创建复杂的正则表达式来匹配不同格式的电话号码和邮箱,并将匹配到的信息复制到剪贴板,方便进一步处理。
2745

被折叠的 条评论
为什么被折叠?



