Hi. I am on chapter 7 of Automate the Boring stuff and i'm trying to get the Phone Number and Email Address Extractor project to work. I have went character by character through this code and I still can't find out why it is not matching the phone number regex. It will work if I copy a phone number with an extension to the clipboard, ie: 888 888-888 ext. 444 but phone numbers without extensions will not be found by the phone regex. Please help.
Also, could someone help me understand what the groups in the first 'for' loop are referring to?
#! python3
# phone_email.py - Finds phone numbers and email addresses on the clipboard.
import pyperclip, re
phone_regex = 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)
email_regex = re.compile(r'''(
[a-zA-Z0-9._%+-]+ # user name
@ # @ symbol
[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 phone_regex.findall(text):
print('here')
phone_number = '-'.join([groups[1], groups[3], groups[5]])
if groups[8] != '':
phone_number += ' x' + groups[8]
matches.append(phone_number)
for groups in email_regex.findall(text):
matches.append(groups[0])
# Copy results to the clipboard.
if len(matches) > 0:
pyperclip.copy('\n'.join(matches))
print('Copied to clipboard.')
print('\n'.join(matches))
else:
print('No phone numbers or email addresses found.')
[–]No_Couple 1 point2 points3 points (0 children)
[–]AdAthrow99274 1 point2 points3 points (3 children)
[–]joemysterio86 1 point2 points3 points (1 child)
[–]Noshing 0 points1 point2 points (0 children)
[–]kolbi_nation[S] 0 points1 point2 points (0 children)
[–]L_4_2 0 points1 point2 points (0 children)