Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #! python3
  2. # phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.
  3.  
  4. import pyperclip, re
  5.  
  6. phoneRegex = re.compile(r'''(
  7. (\d{3}|\(\d{3}\))? # area code
  8. (\s|-|\.)? # separator
  9. (\d{3}) # first 3 digits
  10. (\s|-|\.) # separator
  11. (\d{4}) # last 4 digits
  12. (\s*(ext|x|ext.)\s*(\d{2,5}))? # extension
  13. )''', re.VERBOSE)
  14.  
  15. # Create email regex.
  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}){1,2} # dot-something
  21. )''', re.VERBOSE)
  22.  
  23. # Find matches in clipboard text.
  24. text = str(pyperclip.paste())
  25.  
  26. matches = []
  27. for groups in phoneRegex.findall(text):
  28. phoneNum = '-'.join([groups[1], groups[3], groups[5]])
  29. if groups[8] != '':
  30. phoneNum += ' x' + groups[8]
  31. matches.append(phoneNum)
  32. for groups in emailRegex.findall(text):
  33. matches.append(groups[0])
  34.  
  35. # Copy results to the clipboard.
  36. if len(matches) > 0:
  37. pyperclip.copy('\n'.join(matches))
  38. print('Copied to clipboard:')
  39. print('\n'.join(matches))
  40. else:
  41. print('No phone numbers or email addresses found.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement