31-第4步_将所有匹配连接成一个字符串,复制到剪贴板
第4步:将所有匹配连接成一个字符串,复制到剪贴板
现在, E-mail
地址和电话号码已经作为字符串列表放在 matches
中,你希望将它们复制到剪贴板。 pyperclip.copy()
函数只接收一个字符串值,而不是字符串的列表,因此需要在 matches
上调用 join()
方法。
为了更容易看到程序在工作,让我们将所有找到的匹配都输出在命令行窗口上。如果没有找到电话号码或 E-mail
地址,程序应该发出信息告诉用户。
让你的程序看起来像这样:
#! python3
# phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.
--snip--
for groups in emailRegex.findall(text):
matches.append(groups[0])
# Copy results to the clipboard.
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.')