V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
frmongo
V2EX  ›  Python

Python 的正则表达式的两种写法的区别是什么?

  •  
  •   frmongo · 2018-08-25 11:41:09 +08:00 · 3151 次点击
    这是一个创建于 2063 天前的主题,其中的信息可能已经有所发展或是发生改变。

    第一种写法

    import re
    line = "Cats are smarter than dogs"
    m = re.match( r'(.*) are (.*?) .*', line)
    print m.group()
    print m.group(1)
    print m.group(2)
    

    第二种写法

    import re
    pattern = re.compile(r'(.*) are (.*?) .*')
    m = pattern.match("Cats are smarter than dogs")
    print m.group()
    print m.group(1)
    print m.group(2)
    

    这两种写法有些啥区别?为啥要定义两种写法呢?

    11 条回复    2018-08-27 11:49:49 +08:00
    wwqgtxx
        1
    wwqgtxx  
       2018-08-25 11:45:48 +08:00
    def match(pattern, string, flags=0):
    """Try to apply the pattern at the start of the string, returning
    a Match object, or None if no match was found."""
    return _compile(pattern, flags).match(string)

    这是标准库的定义,所以说你的两种写法本质上是一样的
    imn1
        2
    imn1  
       2018-08-25 11:46:40 +08:00
    oop 当然是为了复用啦
    你以为正则都是只用一次就舍弃?
    glacer
        3
    glacer  
       2018-08-25 11:53:33 +08:00 via iPhone   ❤️ 1
    @wwqgtxx compile 一个复用一个不复用
    delectate
        4
    delectate  
       2018-08-25 11:53:41 +08:00
    楼主这明显还是 py2 的写法;第二种是效率更高一点点。
    wocanmei
        5
    wocanmei  
       2018-08-25 13:28:42 +08:00 via iPhone
    第一种每次调用 match 都有一个正则的编译时间,编译是指正则相当于一种简单的语言,需要对其进行解析,形成某种结构比如语法树,才好对字符串进行匹配,第二种是提前对正则进行了编译,而不是每次调用都有,效率比前者高点
    wwqgtxx
        6
    wwqgtxx  
       2018-08-25 14:40:11 +08:00
    @glacer
    @wocanmei
    其实 python3 的 re.py 中_compile()函数内部是有个_cache 的
    https://github.com/python/cpython/blob/3.7/Lib/re.py#L268
    所以并不会每次调用都会编译一遍
    chenxytw
        7
    chenxytw  
       2018-08-25 16:13:58 +08:00
    @wwqgtxx 其实 python2 就有了,只是那时候是简单的计数淘汰,当达到计数上限时,就把所有的编译过的 re 都扔了 0 0
    PulpFunction
        8
    PulpFunction  
       2018-08-25 17:50:25 +08:00
    俩种写法书上看过,好像只推荐了其中一种
    PythonAnswer
        9
    PythonAnswer  
       2018-08-26 09:23:03 +08:00 via iPhone
    第二种更好。
    wizardoz
        10
    wizardoz  
       2018-08-26 22:37:56 +08:00
    就这个例子来说没啥区别,因为两种都是编译一次用了一次。但是如果同一个正则式反复用的话,可以调用一个 compile,然后反复用 pattern.match 可以减少多次 compile 的时间。
    frostming
        11
    frostming  
       2018-08-27 11:49:49 +08:00
    第一种,每次你 match 的时候都要执行一遍 pattern=re.compile(r'(.*) are (.*?) .*')
    第二种,你先 compile 好了以后就不用每次都 compile 了,效率更高一点

    你只用一次这个正则没什么区别,多次使用时有区别
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3343 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 12:06 · PVG 20:06 · LAX 05:06 · JFK 08:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.