gpt4 book ai didi

python 检查有效的电子邮件

转载 作者:行者123 更新时间:2023-11-28 21:37:39 30 4
gpt4 key购买 nike

Python Central 发布了此 simple script 2016 年 12 月 29 日检查电子邮件格式是否正确。

import re

def isValidEmail(email):
if len(email) > 7:
if re.match("^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$", email) != None:
return True
return False

if isValidEmail("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="55382c7b3038343c39153238343c397b363a38" rel="noreferrer noopener nofollow">[email protected]</a>") == True:
print("This is a valid email address")
else:
print("This is not a valid email address")

在尝试时,python 3 返回了以下错误消息:

Traceback (most recent call last):
File "~/isValidEmail.py", line 11, in <module>
if isValidEmail("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f89581d69d95999194b89f95999194d69b9795" rel="noreferrer noopener nofollow">[email protected]</a>") == True:
File "~/isValidEmail.py", line 7, in isValidEmail
if re.match("^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$", email) != None:
File "/usr/lib/python3.5/re.py", line 163, in match
return _compile(pattern, flags).match(string)
File "/usr/lib/python3.5/re.py", line 293, in _compile
p = sre_compile.compile(pattern, flags)
File "/usr/lib/python3.5/sre_compile.py", line 536, in compile
p = sre_parse.parse(p, flags)
File "/usr/lib/python3.5/sre_parse.py", line 829, in parse
p = _parse_sub(source, pattern, 0)
File "/usr/lib/python3.5/sre_parse.py", line 437, in _parse_sub
itemsappend(_parse(source, state))
File "/usr/lib/python3.5/sre_parse.py", line 781, in _parse
source.tell() - start)
sre_constants.error: missing ), unterminated subpattern at position 4

如何修复此错误?

最佳答案

右括号 ) 包含在方括号内,因为它之前的方括号未正确转义。

^.+@([?)[a-zA-Z0-9-.]+.
^
v
^.+@(\[?)[a-zA-Z0-9-.]+.

示例:

In[2]: import re
...:
...:
...: def is_valid_email(email):
...: if len(email) > 7:
...: return bool(re.match(
...: "^.+@(\[?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$", email))
...:
In[3]: is_valid_email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89e4f0a7ece4e8e0e5c9eee4e8e0e5a7eae6e4" rel="noreferrer noopener nofollow">[email protected]</a>")
Out[3]: True

关于python 检查有效的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49226806/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com