1 Star 0 Fork 0

巧克力ovo/PythonLearn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
4-homework.py 1.90 KB
一键复制 编辑 原始数据 按行查看 历史
wlt 提交于 3个月前 . 4
import re
def validate_email(email):
pattern=r'^[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,}$'
return bool(re.match(pattern,email))
def classify_email(email):
if email.find(".edu")!=-1 or email.find(".ac.")!=-1:
return "教育机构"
elif email.find(".com")!=-1 or email.find(".org")!=-1 or email.find(".net")!=-1:
return "公司"
else:
return "个人"
def extract_email_parts(email):
username,domain=email.split("@",1)
tld=domain.split(".")[-1]
return {
'用户名': username,
'域名': domain,
'TLD': tld
}
def obfuscate_email(email):
parts=re.split(r'@',email)
username, domain = parts
if len(username) > 1:
obfuscated_username = f"{username[0]}***{username[-1]}"
else:
obfuscated_username = username+"***"
last_dot_index = domain.rfind(".")
second_last_dot_index = domain[:last_dot_index].rfind(".") if last_dot_index != -1 else -1
if second_last_dot_index != -1:
obfuscated_domain = f"**{domain[second_last_dot_index + 1:]}"
else:
obfuscated_domain = f"**{domain}"
return f"{obfuscated_username}@{obfuscated_domain}"
test_emails = [
"user.name@university.ac.edu",
"john.doe+test@example.com",
"invalid.email@.com",
"another_user@sub.domain.org",
"private@gmail.com"
]
for item in test_emails:
cnt=0
print("测试邮箱:",item)
if validate_email(item):
print("验证结果:有效")
else:
print("验证结果:无效")
print("---------------------------------")
continue
print("分类结果:",classify_email(item))
print("提取信息:",end="")
for i,v in extract_email_parts(item).items():
cnt+=1
if cnt==3:
print(i,"=",v)
else:
print(i,"=",v,end=",")
print("脱敏处理:",obfuscate_email(item))
print("---------------------------------")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chocolate-ovo/python-learn.git
git@gitee.com:chocolate-ovo/python-learn.git
chocolate-ovo
python-learn
PythonLearn
master

搜索帮助