#! python3
# phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.
import pyperclip, re
# 创建电话的正则表达式
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))? # 区号可选,444或(444)
(\s|-|\.)? # 分隔符:字符或-或. 可选
(\d{3}) # 三个数字
(\s|-|\.)? # 分隔符:字符或-或. 可选
(\d{4}) # 四个数字
(\s*(ext|x|ext.)\s*(\d{2,5}))? # extension
)''',re.VERBOSE)
# 创建email的正则表达式
emailRegex = re.compile(r'''(
[a-zA-Z0-9._%+-]+ # username
@ # @ symbol
[a-zA-Z0-9.-]+ # domain name
(\.[a-zA-Z]{2,4}) # dot-something
)''', re.VERBOSE)
# 匹配剪切板的文本
text = str(pyperclip.paste())
#pyperclip.paste()函数从剪贴板上的文本获取一个字符串
matches = []
for groups in phoneRegex.findall(text):
phoneNum = '-'.join([groups[1], groups[3], groups[5]])
#findall()返回元组列表,形如:[('415','555','9999'),('212','555','0000')]
if groups[8] != '':
phoneNum += ' x' + groups[8]
#不知道这个groups[8]是什么鬼
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.')
groups变量长这样:
(‘800.420.7240’, ‘800’, ‘.’, ‘420’, ‘.’, ‘7240’)
(‘415.863.9900’, ‘415’, ‘.’, ‘863’, ‘.’, ‘9900’)
(‘415.863.9950’, ‘415’, ‘.’, ‘863’, ‘.’, ‘9950’)
所以groups[0]返回的是整体文本