为什么这段代码不起作用?蟒蛇

时间:2016-12-20 11:55:39

标签: python regex

当剪贴板中的文字没有电子邮件地址或电话号码时,即当预期结果为“未找到”时,代码工作正常 对于其他情况,它不起作用。它显示错误 - AttributeError:'str'对象没有属性'matches'

#! python3
# contactDetails.py - Finds email and phone number from a page

import pyperclip, re

phoneRegex = re.compile(r'(\+\d{2}-\d{10})')     # Phone Number Regex
# 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):
    phoneNum=phoneRegex.findall(text)
    matches.append(phoneNum)
for groups in emailRegex.findall(text):
    matches.append(groups[0])

if len(matches) >0:
    pyperclip.copy('\n'.matches)
    print('Copied to Clipboard:')
    print('\n'.join(matches))
else:
    print('Nothing Found')

1 个答案:

答案 0 :(得分:1)

正如WiktorStribiżew的评论中所提到的,问题在于这一行

    pyperclip.copy('\n'.matches)

特别是,它在这里

'\n'.matches

第一项'\n'是一个字符串对象,没有可以调用的称为匹配的属性。你想要的是做一个.join,就像你之后做了两行一样,即

    pyperclip.copy('\n'.join(matches))
相关问题