我编写了一个程序,用于检查用户针对正则表达式输入的电子邮件地址。当用户输入与正则表达式不匹配的错误电子邮件时,将执行打印语句,告知用户该电子邮件不正确。我想要做的是能够告诉他们电子邮件不正确的原因,换句话说,识别用户输入的电子邮件不匹配的正则表达式部分。我不确定如何做到这一点。
import re
student = []
while True:
email = input("Please enter the email, for eg test@hotmail.com")
if not re.match("^[A-Za-z0-9]{5,25}@{1}[A-Za-z]{5,15}[.]{1}[A-Za-z {3,10}$", email):
print("Sorry incorrect format, please re-enter email")
else:
student.append(email)
print(student)
break
答案 0 :(得分:0)
答案 1 :(得分:0)
有很多规则描述了有效的电子邮件地址。我在此提出的解决方案仅测试您在原始问题^[A-Za-z0-9]{5,25}@{1}[A-Za-z]{5,15}[.]{1}[A-Za-z {3,10}$
我建议对基本表达式进行以下更改:
^[A-Za-z0-9]{5,25}@{1}[A-Za-z]{5,15}[.]{1}[A-Za-z {3,10}$
2 1 2 1 2 3
{1}
量词,因为这些不是必需的A-Z
,并使用不区分大小写的标记]
醇>
结果表达式如下:
^[a-z0-9]{5,25}@[A-Za-z]{5,15}[.][a-z]{3,10}$
^(?=(?:.*?@.*?(?P<TooManyAtSigns>@))?)(?=(?P<NoAtSign>[^@\n]*$)?)(?=(?:[a-z0-9]*?(?P<BadCharacterInUsername>[^a-z0-9@\n]))?)(?=(?:.*?@[a-z.]*?(?P<BadCharacterInDomain>[^a-z.\n]))?)(?=(?:(?:(?P<UsernameTooShort>[a-z0-9.]{0,4})|(?P<UsernameTooLong>[a-z0-9.]{26,}))@)?)(?=(?:.*?@(?:(?P<DomainNameTooShort>[A-Za-z]{0,4})|(?P<DomainNameTooLong>[a-z]{16,}))[.])?)(?=(?:.*?@(?P<DomainNameMissingDot>[^.\n]*)$)?)(?=(?:.*?@.*?[.](?:(?P<TopLevelDomainNameTooShort>[a-z]{0,2})|(?P<TopLevelDomainNameTooLong>[a-z]{11,}) )$)?)(?=(?:.*?(?P<FoundASpace>\ ))?)(?P<email>.*)$
**要更好地查看图像,只需右键单击图像并在新窗口中选择视图
**要更好地查看图像,只需右键单击图像并在新窗口中选择视图
此正则表达式将执行以下操作:
@
个字符.
@
之前找到的第一个无效字符以及@
之后的第一个无效字符@gm.com
或@TheForceIsWithYouAllTheTime.com
旁注gm.com
是通用汽车的有效域名。@gmail.au
或@gmail.WhyIsThisNameSoLong
旁注.au
是有效的顶级名称。现场演示
https://regex101.com/r/cQ0jR6/2
示例文字
RoYoMi@gmail.com
RoY@gmail.com
RoYoMiInternationalManOfMystery@gmail.com
Ro!YoMi@@gmail.com
RoYoMi@gm.com
RoYoMiGmail.com
RoYoMi@gmco!m
RoYoMi@ThisIsNotTheDroidYouWereLookingFor.com
RoYoMi@gmail.co
RoYoMi@gmail.InterGalatic
RoY@@Gma.InterGalatic
RoYoMi@MoreDroidsYouAreNotLookingFor.com
样本匹配
MATCH 1
email [0-16] `RoYoMi@gmail.com`
MATCH 2
UsernameTooShort [17-20] `RoY`
email [17-30] `RoY@gmail.com`
MATCH 3
UsernameTooLong [31-62] `RoYoMiInternationalManOfMystery`
email [31-72] `RoYoMiInternationalManOfMystery@gmail.com`
MATCH 4
TooManyAtSigns [81-82] `@`
BadCharacterInUsername [75-76] `!`
BadCharacterInDomain [81-82] `@`
email [73-91] `Ro!YoMi@@gmail.com`
MATCH 5
DomainNameTooShort [99-101] `gm`
email [92-105] `RoYoMi@gm.com`
MATCH 6
NoAtSign [106-121] `RoYoMiGmail.com`
BadCharacterInUsername [117-118] `.`
email [106-121] `RoYoMiGmail.com`
MATCH 7
BadCharacterInDomain [133-134] `!`
DomainNameMissingDot [129-135] `gmco!m`
email [122-135] `RoYoMi@gmco!m`
MATCH 8
DomainNameTooLong [143-177] `ThisIsNotTheDroidYouWereLookingFor`
email [136-181] `RoYoMi@ThisIsNotTheDroidYouWereLookingFor.com`
MATCH 9
TopLevelDomainNameTooShort [195-197] `co`
email [182-197] `RoYoMi@gmail.co`
MATCH 10
TopLevelDomainNameTooLong [211-223] `InterGalatic`
email [198-223] `RoYoMi@gmail.InterGalatic`
MATCH 11
TooManyAtSigns [228-229] `@`
BadCharacterInDomain [228-229] `@`
UsernameTooShort [224-227] `RoY`
DomainNameTooShort [229-232] `Gma`
TopLevelDomainNameTooLong [233-245] `InterGalatic`
email [224-245] `RoY@@Gma.InterGalatic`
MATCH 12
DomainNameTooLong [253-282] `MoreDroidsYouAreNotLookingFor`
email [246-286] `RoYoMi@MoreDroidsYouAreNotLookingFor.com`