import re
def is_valid_email(addr):
re_email = re.compile(r'([0-9a-zA-Z][0-9a-zA-Z\.]*)@([0-9a-zA-Z]+)\.([0-9a-zA-Z]+)')
emailmatch = re_email.match(addr)
if emailmatch:
return True
return False
addr = '<Tom Paris> [email protected]'
print("%s %s" % (addr, is_valid_email(addr)))
输出
<Tom Paris> [email protected] False
期望输出 True
notepad++可以匹配到,另外请原谅我 markdown 语法是刚学的
1
veelog 2018-02-09 22:39:00 +08:00 via iPhone 1
改成 search,match 表示从头匹配
|
3
twor 2018-02-09 22:49:06 +08:00 1
if emailmatch:
----return True return False 换成 return emailmatch 是不是显得简洁一些? |
4
SingeeKing 2018-02-09 22:57:54 +08:00 1
@pkookp8 正则表达式结尾加上 $
|
6
twor 2018-02-09 23:52:58 +08:00
import re
addr1 = '<tom paris=""> [email protected]' addr2 = '[email protected]' re_email = re.compile(r'([0-9a-zA-Z][0-9a-zA-Z\.]*)@([0-9a-zA-Z]+)\.([0-9a-zA-Z]+)') print(re_email.match(addr1)) print(re_email.match(addr2)) print(re_email.search(addr1)) print(re_email.search(addr2)) ➜ TEST python re.py None <_sre.SRE_Match object at 0x10b198ae0> <_sre.SRE_Match object at 0x10b198ae0> <_sre.SRE_Match object at 0x10b198ae0> |
7
tuzhifu 2018-02-10 11:03:53 +08:00 via iPhone
结尾加$并且控制最多只能为 3 个字符就好
|
8
tennc 2018-02-10 17:02:45 +08:00
www。liaoxuefeng。com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143193331387014ccd1040c814dee8b2164bb4f064cff000
|