Cześć
Mam taki kod:
#! python3
# Find phone numbers and email addresses on the clipboard
import pyperclip
import 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._%+-]+ # username
@ # @ 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):
phone_num = '-'.join([groups[1], groups[3], groups[5]])
if groups[8] != '':
phone_num += ' x' + groups[8]
matches.append(phone_num)
for groups in email_regex.findall(text):
matches.append(groups[0])
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.')
Kopiuje tekst do schowka i szuka maili / nr telefonów w wersji US.
W pierwszej pętli jest taki fragment:
for groups in phone_regex.findall(text):
phone_num = '-'.join([groups[1], groups[3], groups[5]])
if groups[8] != '':
phone_num += ' x' + groups[8]
matches.append(phone_num)
Wiem skąd groups[1]-[3]-[5], ale nie ogarniam o co chodzi z groups[8]. To chyba dotyczy 'extension', ale tutaj chyba jest 6-7 grup(już sam nie wiem xd)?
Chyba, że chodzi o to, że jeśli jest coś poza tym tekstem, który chcę otrzymać to po prostu zamienia go w x?