正则表达式(排除)
我正在开发一个正则表达式函数来验证电子邮件地址是否有效。一切似乎都有效,但此电子邮件地址“[email protected]”应该返回条件中为“false”,因为它包含下划线。以下是我编写的代码。有没有办法可以从搜索中排除下划线(_)?
import re
def emailCheck(email):
result = re.search(r'((A-Za-z0-9.[_])*)@((\d?[a-z]?[A-Z]?)*(\b.com\b|\b.edu\b|\b.org\b))', email)
return bool(result)
I am working on a regex function that verifies if the email address is valid. All seem to work but this email address "[email protected]" should return 'false' in the condition because it contains underscore. The following is the code that I have written. Is there a way I could exclude underscore(_) from my search?
import re
def emailCheck(email):
result = re.search(r'((A-Za-z0-9.[_])*)@((\d?[a-z]?[A-Z]?)*(\b.com\b|\b.edu\b|\b.org\b))', email)
return bool(result)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是
(A-Za-z0-9.[_])*
。这只是任意字符A
、Z
、a
、z
、0< 的可选列表/code>、
9
、-
和_
。您需要一个范围字符类。它应该是
[A-Za-z0-9.]+
并添加开始/结束行元转义。https://regex101.com/r/1Ors2C/1
Your issue is
(A-Za-z0-9.[_])*
. This is just a optional listing of arbitrary charactersA
,Z
,a
,z
,0
,9
,-
, and_
.You need a character class for ranges. It should be
[A-Za-z0-9.]+
and add a start/end line meta escape.https://regex101.com/r/1Ors2C/1