请提供您的邮箱
一位同事向您求助!当用户在公司网站注册时,必须提供一个有效的电子邮箱地址。 公司制定了以下规则来验证邮箱是否有效:
- 邮箱的第一部分可以包含:
- 大写
A-Z或小写a-z字母 - 数字
- 字符:
!、#、%、&、*、$、.
- 大写
- 必须包含
@ - 域名部分:
- 可以包含任意"单词"字符
- 但只能以
.com结尾
本项目要求您编写一个脚本,检查邮箱地址是否符合上述模式。您的同事提供了一份邮箱地址列表作为测试示例。
列表 emails 以及 re 模块已加载到您的会话中。您可以使用 print(emails) 在 IPython Shell 中查看这些邮箱。
本练习是课程的一部分
Python 中的正则表达式
练习说明
- 按描述编写一个用于匹配有效邮箱地址的正则表达式。
- 将该正则表达式用于匹配
emails中的各个元素。 - 为了打印出是否为有效邮箱的提示信息,请补全
.format()语句。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Write a regex to match a valid email address
regex = ____"[____]____@____\.com"
for example in emails:
# Match the regex to the string
if re.____(____, ____):
# Complete the format method to print out the result
print("The email {____} is a valid email".____(email_example=____))
else:
print("The email {____} is invalid".____(email_example=____))