收到一个我不明白的 "unterminated subpattern" 错误

Getting an "unterminated subpattern" error I don't understand

我正在学习 Python 从关于 Python 自动化无聊的东西的 No Starches 一书中。对于一个项目,我们编写了一个脚本来从剪贴板上的文本中抓取 phone 个数字。

我遇到错误(如下所示)。我做错了什么,我该如何解决?

我的代码:

import re

# Phone number regex
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))?             #area code
(\s|-|\.)?                     #seperator
(\d{3}\)                       #first 3 digits
(\s|-|\.)                      #seperator
(\d{4})                        #last 4 digits
(\s*(ext|x|ext.)\s*(\d{2,5}))? #extension
)''', re.VERBOSE)

错误:

Traceback (most recent call last):
  File "*path snipped for privacy*/Automate the Boring Stuff with Python/Chapter 7 project phoneAndEmail.py", line 7, in <module>
    phoneRegex = re.compile(r'''(
  File "/Applications/Mu Editor.app/Contents/Resources/Python/lib/python3.8/re.py", line 252, in compile
    return _compile(pattern, flags)
  File "/Applications/Mu Editor.app/Contents/Resources/Python/lib/python3.8/re.py", line 304, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/Applications/Mu Editor.app/Contents/Resources/Python/lib/python3.8/sre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)
  File "/Applications/Mu Editor.app/Contents/Resources/Python/lib/python3.8/sre_parse.py", line 948, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "/Applications/Mu Editor.app/Contents/Resources/Python/lib/python3.8/sre_parse.py", line 443, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "/Applications/Mu Editor.app/Contents/Resources/Python/lib/python3.8/sre_parse.py", line 836, in _parse
    raise source.error("missing ), unterminated subpattern",
re.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

phoneRegex = re.compile(r'''(
    (\d{3}|\(\d{3}\))? # area code
    (\s|-|\.)?         # separator
    (\d{3})              # first 3 digits
    (\s|-|\.)          # separator
    (\d{4})              # last 4 digits
    (\s*(ext|x|ext.)\s*(\d{2,5}))?  # extension
    )''', 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}){1,2} # 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])

错误在 phoneRegex 模式的这一部分:

(\d{3}\)

开始的 ( 是一个组的开始,但是结束的 )\ 转义,使其成为文字 ) 而不是结束组的。

因此,组的开头 ( 永远不会被结尾 ) 平衡(即,组,一个子模式,是“未终止的”)。

您应该将 \) 更改为 )