是否有一个好方法来检查表单输入使用正则表达式,以确保它是一个正确的风格的电子邮件地址?从昨晚开始搜索,每个人都回答了关于这个话题的问题,如果它是一个子域名的电子邮件地址,似乎也有问题。
当前回答
"^[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}$"
其他回答
我还没有在这里的一堆自定义正则表达式答案中看到答案,但是……
存在一个名为py3-validate-email validate_email的python库,它有3个级别的电子邮件验证,包括询问有效的SMTP服务器电子邮件地址是否有效(不发送电子邮件)。
安装
python -m pip install py3-validate-email
基本用法:
from validate_email import validate_email
is_valid = validate_email(email_address='example@example.com', \
check_regex=True, check_mx=True, \
from_address='my@from.addr.ess', helo_host='my.host.name', \
smtp_timeout=10, dns_timeout=10, use_blacklist=True)
对于那些对肮脏细节感兴趣的人,validate_email.py (source)旨在忠实于RFC 2822。
我们所做的只是将输入字符串与1进行比较 庞大的正则表达式。但是构建那个regexp 通过组装它,可以更容易地确保其正确性 从RFC定义的“令牌”。每一个符号都是 在附带的单元测试文件中测试。
你可能需要pyDNS模块来检查SMTP服务器
pip install pyDNS
或者来自Ubuntu
apt-get install python3-dns
如果你想从一个长字符串或文件中取出邮件,那么试试这个。
([^@|\s]+@[^@]+\.[^@|\s]+)
注意,当你的电子邮件地址前后都有空格时,这是有效的。如果你没有空间或有一些特殊的字符,那么你可以尝试修改它。
工作的例子:
string="Hello ABCD, here is my mail id example@me.com "
res = re.search("([^@|\s]+@[^@]+\.[^@|\s]+)",string,re.I)
res.group(1)
这将从该字符串中删除example@me.com。
另外,注意这可能不是正确答案……但我把它贴在这里是为了帮助像我这样有特殊要求的人
我发现了一个很好的(经过测试的)方法来检查有效的电子邮件地址。我把代码粘贴在这里:
# 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)
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.
发现电子邮箱:
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..
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录