-5
當剪貼板中的文本沒有電子郵件地址或電話號碼時,代碼工作正常,即當預期結果爲「Nothing Found」時 對於其他情況,它不起作用。它顯示錯誤 - AttributeError的:「STR」對象沒有屬性「匹配」爲什麼這段代碼不工作? Python
#! 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')
請參見''\ n'.matches' - 你肯定要加入列表中的項目。 –
更改標題和[創建最小,完整和可驗證的示例。](http://stackoverflow.com/help/mcve)這種標題容易被標記和關閉。 – MYGz