Regex телефон и ошибка вывода извлечения электронной почты - python - PullRequest
0 голосов
/ 12 мая 2018

Следующая программа используется для извлечения номера телефона и электронной почты из буфера обмена. Проблема в том, что всякий раз, когда я копирую что-то и запускаю этот код, он выдает результат, как показано ниже:

screenshot

Вот мой код:

import pyperclip,re
phnRegex=re.compile(r'''(\+\d\d)?       #country code
                        (-|\s|\.)?      #seperator
                            (\d{10})    #numbers
                        ''',re.VERBOSE)

emailRegex=re.compile(r'''[a-zA-Z0-9._+%-]+           #username
                            @
                            [a-zA-Z0-9_-]+              #domain
                            (\.[a-zA-Z]{2,4})           #dot-something
                            ''',re.VERBOSE)
text=str(pyperclip.paste())
matches=[]
for groups in phnRegex.findall(text):
    phoneNum='-'.join(groups[1],groups[3])
    matches.append(phoneNum)
for groups in emailRegex.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.')

Любая помощь приветствуется.

Образец текста, который копируется:

Общие запросы: flyingreturnsbase.ai@iclployalty.com
Недостающие мили / Ретро кредит на AI: airindiaretros.ai@iclployalty.com
Недостающие мили / Ретро кредит на Star Partners: starallianceretros.ai@iclployalty.com
Участники Silver Edge: silveredge.ai@iclployalty.com
Золотой край Члены: goldenedge.ai@iclployalty.com
Клуб Махараджи Члены: maharajahclub.ai@iclployalty.com

1 Ответ

0 голосов
/ 14 мая 2018

изменить группу захвата в emailRegex на (?:\.[a-zA-Z]{2,4}) #dot-something

также измените циклы for в коде на

for groups in phnRegex.findall(text):
    matches.append(groups[0] + '-' + groups[2])
for groups in emailRegex.findall(text):
    matches.append(groups)

Демо

...