Advertisement
mengyuxin

meng.phoneAndEmail.py

Jan 2nd, 2018
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. #! python3
  2. # phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.
  3. # http://www.nostarch.com/contactus.htm
  4. # http://docs.python.org/3/library/re.html
  5. # http://www.regular-expression.info/
  6.  
  7. import pyperclip # Clipboard 剪贴板
  8. import re        # Regular Expression 正则表达式
  9.  
  10. # Create phone regex.
  11. # 为电话号码创建一个正则表达式
  12. phoneRegex = re.compile(r'''(
  13.    (\d{3}|\(\d{3}\))?                    # area code
  14.    (\s|-|\.)?                          # separator
  15.    (\d{3})                             # first 3 digits
  16.    (\s|-|\.)                           # separator
  17.    (\d{4})                             # last 3 digits
  18.    (\s*(ext|x|ext\.)\s*(\d{2,5}))?     # extension
  19.    )''', re.VERBOSE)
  20.  
  21. # Create email regex.
  22. # 为E-mail地址创建一个正则表达式
  23. emailRegex = re.compile(r'''(
  24.    [a-zA-Z0-9._%+-]+       # username
  25.    @                       # @ symbol
  26.    [a-zA-Z0-9.-]+          # domain name
  27.    (\.[a-zA-Z]{2,4})       # dot something
  28.    )''', re.VERBOSE)       # for complex regular expression
  29.  
  30. # Find matches in clipboard text.
  31. # 在剪贴板文本中找到所有匹配
  32. text = str(pyperclip.paste())
  33. matches = []
  34. for groups in phoneRegex.findall(text):
  35.     phoneNum = '-'.join([groups[1], groups[3], groups[5]])
  36.     if groups[8] != '':
  37.         phoneNum += ' x' + groups[8]
  38.     matches.append(phoneNum)
  39.  
  40. for groups in emailRegex.findall(text):
  41.     matches.append(groups[0])
  42.    
  43. # copy Results to the clipboard.
  44. # 复制结果到剪贴板
  45. if len(matches) > 0:
  46.     pyperclip.copy('\n'.join(matches))
  47.     print('Copied to clipboard:')
  48.     print('\n'.join(matches))
  49. else:
  50.     print('No phone numbers or email addresses found.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement