天天看點

7.15 電話号碼和Email位址提取程式

import pyperclip
import re

phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))?
(\s|-|\.)?
(\d{3})
(\s|-|\.)
(\d{4})
(\s*(ext|x|ext\.)\s*(\d{2,5}))?
)''', re.VERBOSE)

emailRegex = re.compile(r"""(
[a-zA-Z0-9%+-]+
@
[a-zA-Z0-9.-]+
(\.[a-zA-z]{2,4})
)""", re.VERBOSE)

text = pyperclip.paste()
matches = []
for groups in phoneRegex.findall(text):
    phoneNum = '-'.join([groups[1], groups[3], groups[5]])
    if groups[8] != ' ':
        phoneNum += ' x' + groups[8]
    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 number or email address found.')

           

示例.txt

Contact Us

No Starch Press, Inc.
245 8th Street
San Francisco, CA 94103 USA
Phone: 800.420.7240 or +1 415.863.9900 (9 a.m. to 5 p.m., M-F, PST)
Fax: +1 415.863.9950

Reach Us by Email

General inquiries: [email protected].com
Media requests: [email protected].com
Academic requests: [email protected].com (Further information)
Conference and Events: [email protected].com
Help with your order: [email protected].com
Reach Us on Social Media
Twitter
Facebook
Instagram
Linkedin
Pinterest
           

繼續閱讀