【发布时间】:2019-09-16 05:09:07
【问题描述】:
我正在通过 Al Sweigart 的自动化 udemy 上无聊的东西课程第 29 课来学习正则表达式。我收到一条错误消息:“位置 414 的括号不平衡(第 12 行,第 1 列)”
该代码旨在使用正则表达式提取电话号码和电子邮件地址。
我已经尝试计算括号并为电子邮件正则表达式取出顶部和底部括号。
#! python3
import re, pyperclip
# Done - TODO: create a regex object for phone numbers
phoneRegex = re.compile(r'''
# Types of number 415-555-0000, 555-0000, (415) 555-0000, 555-0000 ext 12345,
# ext. 12345, x12345
(
((\d\d\d) | (\(\d\d\d\)))? # area code (optional)
(\s|-) # first separator
\d\d\d # first 3 digits
- # separator
\d\d\d\d # last 4 digits
((ext(\.)?\s)|x) # extension word part (optional)
(\d{2,5}))? # extension number part (optional)
)
''', re.VERBOSE)
# TODO: Create a regex for email addresses
emailRegex = re.compile (r'''
# some.+_thing@(\d{2,5}))?.com
[a-zA-Z0-9_.+]+ # name part - created non default regular expression class
# to capture any character a-z lowercase, A-Z upper case, numbers 0-9, characters _.+
@ # @ symbol
[a-zA-Z0-9_.+]+ # domain name part
''', re.VERBOSE)
# TODO: Get the text off the clipboard
text = pyperclip.paste()
# TODO: Extract the email/phone from this text
extractedPhone = phoneRegex.findall(text) # creates one string for each group ()
# Make sure desired regex is all in one group ()
extractedEmail = emailRegex.findall(text)
print (extractedPhone)# temporary print function to see if code works
print (extractedEmail)
给出这个错误:
Traceback(最近一次调用最后一次): 文件“C:\Users*\Desktop\Education\computer science\自动化无聊的东西\programs\lesson 29 phone and email regex.py”,第 18 行, 在 ''', re.VERBOSE) 文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\re.py”,行 234,在编译中 返回_compile(模式,标志) 文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\re.py”,行 286,在_编译 p = sre_compile.compile(模式,标志) 文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\sre_compile.py”, 第 764 行,在编译中 p = sre_parse.parse(p, 标志) 文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\sre_parse.py”, 第 944 行,解析中 raise source.error("不平衡括号") re.error:位置 414 处的不平衡括号(第 12 行,第 1 列)
【问题讨论】:
标签: regex python-3.x