【发布时间】:2020-08-05 00:27:12
【问题描述】:
这是一个找到的项目@https://automatetheboringstuff.com/2e/chapter7/ 它会在剪贴板上的文本中搜索电话号码和电子邮件,然后将结果再次复制到剪贴板。
如果我理解正确,当正则表达式包含组时,findall() 函数会返回一个元组列表。每个元组将包含匹配每个正则表达式组的字符串。
现在这是我的问题:据我所知,phoneRegex 上的正则表达式仅包含 6 个组(在代码上编号)(所以我希望元组的长度为 6)
但是当我打印元组时,我得到长度为 9 的元组
('800-420-7240', '800', '-', '420', '-', '7240', '', '', '')
('415-863-9900', '415', '-', '863', '-', '9900', '', '', '')
('415-863-9950', '415', '-', '863', '-', '9950', '', '', '')
我错过了什么?
#! python3
# phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.
import pyperclip, re
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area code (first group?)0
(\s|-|\.)? # separator 1
(\d{3}) # first 3 digits 2
(\s|-|\.) # separator 3
(\d{4}) # last 4 digits 4
(\s*(ext|x|ext.)\s*(\d{2,5}))? # extension 5
)''', re.VERBOSE)
# Create email regex.
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())
matches = []
for groups in phoneRegex.findall(text):
print(groups)
phoneNum = '-'.join([groups[1], groups[3], groups[5]])
if groups[8] != '':
phoneNum += ' x' + groups[8]
matches.append(phoneNum)
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.')
【问题讨论】:
标签: python-3.x findall python-re