我目前正在通过udemy上的python课程使无聊的东西自动化,并且不明白为什么return语句比讲师打印出大量的信息。
据我所知,我的代码与他的代码相同,只是我在括号中加上了代码行(注释#个前3位数字,以及#个后4位数字),因为如果没有它,该程序仅返回带空格和所有带空格的数字。
#: python3
import re, pyperclip
# Create a regex for phone numbers
phoneRegex = re.compile(r'''
((\d\d\d)|(\(\d\d\d\)))? # area code <optional>
(\s|-) # first seperator
(\d\d\d) # first 3 digitis
- # seperator
(\d\d\d\d) # last 4 digits
(((ext(\.)?\s)|x) # extension word-part<optional>
(\d{2,5}))? # extension number-part<optional> 2,5 is to signify that it can be 2-5 digits
''', re.VERBOSE)
# TODO:: Create a regex object for email addresses
emailRegex = re.compile(r'''
# we will make it search for emails that contain any numbers, letters plus or period symbols
[a-zA-Z0-9_.+]+ # name part
@ # @ symbol
[a-zA-Z0-9_.+]+ # domain part
''', re.VERBOSE)
# Get the text off the clipboard
text = pyperclip.paste()
# Extract the email/phone from this text
extractedPhone = phoneRegex.findall(text)
extractedEmail = emailRegex.findall(text)
print(extractedPhone)
print(extractedEmail)
在运行程序之前,我正在复制的文本可以在此处找到。请注意,由于不再使用指向他的pdf的链接,因此讲师有所不同。 https://docs.google.com/document/d/1WDlqPyCqETtlwXpxc_HL9zissIDMuNx6aysRcI-kTqw/edit?usp=sharing