[英]How can I remove unterminated string while fetching details from raw string
While writing code to search for USA phone number patterns which may or may not start with first 3 numbers like (415) or 415 (eg full number like (415)-555-1234 ) encountered the error untermianted subpattern.在编写代码以搜索美国电话号码模式时,可能会或可能不会以 (415) 或 415 之类的前 3 个数字(例如 (415)-555-1234 之类的完整数字)开头的美国电话号码模式遇到错误 untermianted 子模式。 Code:-
代码:-
#! python3
import re, pyperclip
#TODO: Create a regex for phone numbers
phoneRegex = re.compile(r'''
((\d\d\d)|(\(\d\d\d\)))? # area code (optional) #this is for pattersn where 415 or (415) for which we puta ? at the end indicating appear 0 or 1 times
(\s|-) # first separator either - or a space
\d\d\d # first 3 digits
- #separator
\d\d\d\d # last 4 digits
(((ext(\.)?\s|x) #extension number part may be 2 till 5 characvters long
(\d{2,5}))? # as this entire number or extension part can be optional
''',re.VERBOSE)
#TODO: Create a regex for email addresses
#EMAIL part is working fine and tested on 10/08/22
emailRegex = re.compile(r'''
#some.+_things@(\d{2,5}))?.com
[a-zA-Z0-9_.+]+ #name part
@ #@ symbol
[a-zA-Z0-9_.+]+ #Domain name part
''',re.VERBOSE)
#TODO: Get text off the clipboard
text = pyperclip.paste()
#TODO: Extract the email/phone from this text
extractedPhone = phoneRegex.findall(text)
extractedEmail = emailRegex.findall(text)
#TODO: Copy the extracted email/phone to the clipboard
print(extractedPhone)
print(extractedEmail)
error thrown = re.error: missing ), unterminated subpattern at position 266 (line 7, column 1)抛出的错误 = re.error: missing),position 266 处的未终止子模式(第 7 行,第 1 列)
One of the parenthesis is not closed here:括号之一在这里没有关闭:
(( (ext(\.)?\s|x)
(\d{2,5}) )?
You can decide how to edit it depending on what you want.您可以根据需要决定如何编辑它。
Or you can just try removing it:或者您可以尝试删除它:
( (ext(\.)?\s|x)
(\d{2,5}) )?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.