推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
pkookp8
V2EX  ›  Python

初学 Python ,求解释失败的原因

  •  
  •   pkookp8 · Feb 9, 2018 · 3762 views
    This topic created in 3150 days ago, the information mentioned may be changed or developed.

    
    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 语法是刚学的 Alt text

    veelog
        1
    veelog  
       Feb 9, 2018 via iPhone   ❤️ 1
    改成 search,match 表示从头匹配
    pkookp8
        2
    pkookp8  
    OP
       Feb 9, 2018
    @veelog 了解。另外改为 search 又有一个问题,非法+合法+非法的字符串也会被识别为合法,这又该怎么屏蔽
    twor
        3
    twor  
       Feb 9, 2018   ❤️ 1
    if emailmatch:
    ----return True
    return False
    换成
    return emailmatch

    是不是显得简洁一些?
    SingeeKing
        4
    SingeeKing  
       Feb 9, 2018   ❤️ 1
    @pkookp8 正则表达式结尾加上 $
    pkookp8
        5
    pkookp8  
    OP
       Feb 9, 2018
    @twor 对哦....完全没想到可以直接返回
    另外我这里的正则表达式也写的很垃圾= =!感觉整个函数都是槽点
    twor
        6
    twor  
       Feb 9, 2018
    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>
    tuzhifu
        7
    tuzhifu  
       Feb 10, 2018 via iPhone
    结尾加$并且控制最多只能为 3 个字符就好
    tennc
        8
    tennc  
       Feb 10, 2018
    www。liaoxuefeng。com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143193331387014ccd1040c814dee8b2164bb4f064cff000
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1244 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 17:08 · PVG 01:08 · LAX 10:08 · JFK 13:08
    ♥ Do have faith in what you're doing.