How can I remove unterminated string while fetching details from raw string

128 Views Asked by At

While writing code to search for USA phone number patterns which may or may not start with first 3 numbers like (415) or 415 (e.g full number like (415)-555-1234 ) encountered the error untermianted subpattern. 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)

1

There are 1 best solutions below

1
Vladimir Fokow On

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})      )?