我一直在尝试查找代码以匹配项目电子邮件中的正则表达式。这些是要求:
电子邮件必须采用acct @ domain
的形式
我已经找到了代码中的acct部分:
if re.search("^[a-zA-z0-9]+[a-zA-z0-9-_]*$|^[a-zA-z0-9]+[a-zA-z0-9-_]+[\.]{1}[a-zA-z0-9]{2,}$", email):
print "valid!"
也是域名:
if re.search("^[a-zA-z0-9]+[a-zA-z0-9-_]+[\.]{1}[a-zA-z0-9]{2,}$", email):
print "valid!"
我的问题是我无法弄清楚如何将它们组合在一起并放上一个@符号
我尝试了以下但是它似乎没有效果。
if re.search("(^[a-zA-z0-9]+[a-zA-z0-9-_]*$|^[a-zA-z0-9]+[a-zA-z0-9-_]+[\.]{1}[a-zA-z0-9]{2,}$)@(^[a-zA-z0-9]+[a-zA-z0-9-_]+[\.]{1}[a-zA-z0-9]{2,}$)", email):<br>
print "valid!
&#34;
它没有工作!我无法让它永远匹配。如果您有建议使代码不那么引人注目,请告诉我!
答案 0 :(得分:1)
摆脱两组中的锚点并将其应用于整个组
if re.search(r"^(?:[a-zA-Z0-9]+[a-zA-Z0-9-]*|[a-zA-Z0-9]+[a-zA-Z0-9-]+\.[a-zA-Z0-9]{2,})@[a-zA-Z0-9]+[-\w]+\.[a-zA-Z0-9]{2,}$", email):
print "valid!"
所做的更改
锚点^
和$
适用于整个正则表达式
[\.]{1}
可简化为\.
,因为它仅匹配.
的一个
[a-zA-z0-9-_]
可简化为[-\w]
答案 1 :(得分:1)
使用non-capturing group合并两个正则表达式。
if re.search(r"^(?:[a-zA-Z0-9]+[a-zA-Z0-9-]*|[a-zA-Z0-9]+[a-zA-Z0-9-]+[.][a-zA-Z0-9]{2,})@[a-zA-Z0-9]+[a-zA-Z0-9-_]+[.][a-zA-Z0-9]{2,}$", email):
print "valid"
正则表达式:
^ the beginning of the string
(?: group, but do not capture:
[a-zA-Z0-9]+ any character of: 'a' to 'z', 'A' to
'Z', '0' to '9' (1 or more times)
[a-zA-Z0-9-]* any character of: 'a' to 'z', 'A' to
'Z', '0' to '9', '-' (0 or more times)
| OR
[a-zA-Z0-9]+ any character of: 'a' to 'z', 'A' to
'Z', '0' to '9' (1 or more times)
[a-zA-Z0-9-]+ any character of: 'a' to 'z', 'A' to
'Z', '0' to '9', '-' (1 or more times)
[.] any character of: '.'
[a-zA-Z0-9]{2,} any character of: 'a' to 'z', 'A' to
'Z', '0' to '9' (at least 2 times)
) end of grouping
@ '@'
[a-zA-Z0-9]+ any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9' (1 or more times)
[a-zA-Z0-9-_]+ any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9', '-', '_' (1 or more times)
[.] any character of: '.'
[a-zA-Z0-9]{2,} any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9' (at least 2 times)
$ before an optional \n, and the end of the
string
答案 2 :(得分:1)
以下是可以验证所有标准的正则表达式,我希望它也更有效。
^(?![\W_])((?:([\w-]{2,})\.?){1,})(?<![\W_])@(?![\W_])(?=[\w.-]{5,})(?=.+\..+)((?:([\w-]{2,})\.?){1,})(?<![\W_])$
这是regex demo。