是否有一个好方法来检查表单输入使用正则表达式,以确保它是一个正确的风格的电子邮件地址?从昨晚开始搜索,每个人都回答了关于这个话题的问题,如果它是一个子域名的电子邮件地址,似乎也有问题。
当前回答
发现电子邮箱:
import re
a=open("aa.txt","r")
#c=a.readlines()
b=a.read()
c=b.split("\n")
print(c)
for d in c:
obj=re.search(r'[\w.]+\@[\w.]+',d)
if obj:
print(obj.group())
#for more calcification click on image above..
其他回答
在电子邮件输入上使用此过滤器掩码: emailMask: / (\ w .\-@'"!#$%&'*+/=?^ _{|} ~) /我
The only really accurate way of distinguishing real, valid email addresses from invalid ones is to send mail to it. What counts as an email is surprisingly convoluted ("John Doe" <john.doe@example.com>" actually is a valid email address), and you most likely want the email address to actually send mail to it later. After it passes some basic sanity checks (such as in Thomas's answer, has an @ and at least one . after the @), you should probably just send an email verification letter to the address, and wait for the user to follow a link embedded in the message to confirm that the email was valid.
我发现了一个很好的(经过测试的)方法来检查有效的电子邮件地址。我把代码粘贴在这里:
# here i import the module that implements regular expressions
import re
# here is my function to check for valid email address
def test_email(your_pattern):
pattern = re.compile(your_pattern)
# here is an example list of email to check it at the end
emails = ["john@example.com", "python-list@python.org", "wha.t.`1an?ug{}ly@email.com"]
for email in emails:
if not re.match(pattern, email):
print "You failed to match %s" % (email)
elif not your_pattern:
print "Forgot to enter a pattern!"
else:
print "Pass"
# my pattern that is passed as argument in my function is here!
pattern = r"\"?([-a-zA-Z0-9.`?{}]+@\w+\.\w+)\"?"
# here i test my function passing my pattern
test_email(pattern)
发现这是一个实用的实现:
^[^@\s]+@[^@\s]+\.[^@\s]+$
这通常是使用正则表达式解决的。然而,有许多不同的解决方案。这取决于您需要的严格程度,以及您是否有自定义的验证要求,或者是否接受任何有效的电子邮件地址。
参考页面:http://www.regular-expressions.info/email.html
推荐文章
- 如何从psycopg2游标获得列名列表?
- Python中dict对象的联合
- 如何有效地比较两个无序列表(不是集合)?
- 如何在交互式Python中查看整个命令历史?
- 如何显示有两个小数点后的浮点数?
- 如何用OpenCV2.0和Python2.6调整图像大小
- 在每个列表元素上调用int()函数?
- 如何检查一行是否为空白使用正则表达式
- RegEx以确保字符串至少包含一个小写字符、大写字符、数字和符号
- 当使用代码存储库时,如何引用资源的相对路径
- 如何在Flask-SQLAlchemy中按id删除记录
- 在Python中插入列表的第一个位置
- Python Pandas只合并某些列
- Java Regex捕获组
- 如何在一行中连接两个集而不使用“|”