下面的代码完全照着本书中的项目示例,除非有错误。当我将一些代码复制到剪贴板,然后在IDLE中运行该程序时,外壳会显示以下内容。请帮助我进行故障排除。
Traceback (most recent call last):
File "C:/Python3/phoneAndEmail.py", line 23, in <module>
)''', re.VERBOSE)
File "C:\Users\Tyler\AppData\Local\Programs\Python\Python36\lib\re.py", line 233, in compile
return _compile(pattern, flags)
File "C:\Users\Tyler\AppData\Local\Programs\Python\Python36\lib\re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\Users\Tyler\AppData\Local\Programs\Python\Python36\lib\sre_compile.py", line 562, in compile
p = sre_parse.parse(p, flags)
File "C:\Users\Tyler\AppData\Local\Programs\Python\Python36\lib\sre_parse.py", line 856, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
File "C:\Users\Tyler\AppData\Local\Programs\Python\Python36\lib\sre_parse.py", line 415, in _parse_sub
itemsappend(_parse(source, state, verbose))
File "C:\Users\Tyler\AppData\Local\Programs\Python\Python36\lib\sre_parse.py", line 766, in _parse
source.tell() - start)
sre_constants.error: missing ), unterminated subpattern at position 0 (line 1, column 1)
#! python3
# phoneAndEmail.py - Finds phone numbers and email addresses on the
#clipboard.
import pyperclip, re
# Create phone regex
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
(\d{3}) # 3 digits
(\s|-|\.)? # separator
(\d{4}) # 4 digits
(\s*(ext|x|ext.)\s*(\d{2,5}))? # ext
)''', re.VERBOSE)
# Create email regex
emailRegex = re.compile(r'''(
[a-zA-Z0-9._%+-]+ # username
@ # @
[a-zA-Z0-9.-]+ # domain name
(\.[a-zA-Z]{2-4} # dot something
)''', re.VERBOSE)
# Find matches in clipboard text.
text = str(pyperclip.paste())
matches = []
for groups in phoneRegex.findall(text):
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 clipboard
if len(matches) > 0:
pyperclip.copy('\n'.join(matches))
print('Copied to Clipboard:')
print('\n'.join(matches))
else:
print('No phone numbers or emails found.')
答案 0 :(得分:2)
看看错误:
sre_constants.error:缺少),位置0(第1行,第1列)的未终止子模式
emailRegex = re.compile(r'''(
[a-zA-Z0-9._%+-]+ # username
@ # @
[a-zA-Z0-9.-]+ # domain name
(\.[a-zA-Z]{2-4} # dot something
)''', re.VERBOSE)
这意味着您缺少)
。有两个(
,但只有一个)