-1

The following program is used to extract phone number and email from the clipboard. The problem is whenever I copy something and run this code it gives the output as shown below:

screenshot

Here is my code:

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.')

Any help is appreciated.

Sample Text that is copied is:

General Queries: flyingreturnsbase.ai@iclployalty.com
Missing Miles / Retro Credit on AI: airindiaretros.ai@iclployalty.com
Missing Miles / Retro credit on Star Partners: starallianceretros.ai@iclployalty.com
Silver Edge Members: silveredge.ai@iclployalty.com
Golden Edge Members: goldenedge.ai@iclployalty.com
The Maharajah Club Members: maharajahclub.ai@iclployalty.com

1 Answers1

0

change the capturing group in emailRegex to (?:\.[a-zA-Z]{2,4}) #dot-something

also, modify the for loops in the code to

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

Demo

Matt.G
  • 3,586
  • 2
  • 10
  • 23