arten_1337

Phone number and email regex

Jul 22nd, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. # 1 /usr/bin/python3
  2. # phoneandemail.py- Finds all phone numbers and email addresses on the clipboard.
  3.  
  4. import pyperclip
  5. import re
  6. # regex for phonenumber
  7. phoneregex = re.compile(r'''(
  8. (\d{3}|\(\d{3}\))?                      #areacode
  9. (\s|-|\.)?                              #seperator
  10. (\d{3})                                 #first 3 digits
  11. (\s|-|\.)?                              #seperator
  12. (\d{4})                                 #last four digits
  13. (\s*(ext|x|ext.)\s*(\d{2,5}))?          #extension
  14. )''', re.VERBOSE)
  15. # regex for email
  16. emailregex = re.compile(r'''(
  17. [a-zA-Z0-9._%+-]+                       #username
  18. @                                       #@ symbol
  19. [a-zA-z0-9.-]+                          #domain name
  20. (\.[a-zA-z]{2,4})                       #dot something
  21. )''', re.VERBOSE)
  22. # Find Matches in the Clipboard
  23. text = str(pyperclip.paste())
  24. matches = []
  25. for groups in phoneregex.findall(text):
  26.     phonenum = '-'.join([groups[1], groups[3], groups[5]])
  27.     if groups[8] != '':
  28.         phonenum += ' x'+groups[8]
  29.     matches.append(phonenum)
  30. for groups in emailregex.findall(text):
  31.     matches.append(groups[0])
  32. # copy results to the clipboard
  33. if len(matches) > 0:
  34.     pyperclip.copy('\n'.join(matches))
  35.     print('Copied to clipboard')
  36.     print('\n'.join(matches))
  37. else:
  38.     print('No phone numbers or emails were found')
Advertisement
Add Comment
Please, Sign In to add comment