正则表达式的简化? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
ispinfx

正则表达式的简化?

  •  
  •   ispinfx Dec 18, 2020 2615 views
    This topic created in 1958 days ago, the information mentioned may be changed or developed.

    有工具可以把正则简化(如下面的r2简单成r1)的吗?或者re.compile会简化正则表达式(结果看起来不会)吗?

    # -*- coding: utf-8 -*- import itertools import re string = "".join(map(str, range(1000000))) r1 = r"[13579]{3}" r2 = rf'{"|".join(map("".join, itertools.product("13579", repeat=3)))}' r1 = re.compile(r1) r2 = re.compile(r2) match1 = re.findall(r1, string) match2 = re.findall(r2, string) assert sorted(match1) == sorted(match2) 

    结果:

    In [78]: %timeit re.findall(r1, string) 167 ms ± 520 s per loop (mean ± std. dev. of 7 runs, 10 loops each) In [79]: %timeit re.findall(r2, string) 1.08 s ± 2.69 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 
    5 replies    2020-12-18 16:20:24 +08:00
    f6x
        1
    f6x  
       Dec 18, 2020
    这个 r2 写的真牛
    ispinfx
        2
    ispinfx  
    OP
       Dec 18, 2020
    @f6x 只是一个例子…
    abersheeran
        3
    abersheeran  
       Dec 18, 2020
    mark……如果真的有,给我也说一声。
    geelaw
        4
    geelaw  
       Dec 18, 2020 via iPhone   2
    很可惜正则表达式最小化问题是 PSPACE-complete,所以不应该期待高效算法。

    另外,最小化 NFA 也是 PSPACE-complete,而最小化 DFA 则是多项式时间可解的。然而从正则表达式算出最小 DFA 的意义也不是很大,除非是做编译器的 lexer从正则表达式到 DFA,规模可以以指数级增长,只有精心设计的正则语言才适合用 DFA 表示。
    Lemeng
        5
    Lemeng  
       Dec 18, 2020
    有,就太好了
    About     Help     Advertise     Blog     API     FAQ     Solana     3239 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 40ms UTC 13:22 PVG 21:22 LAX 06:22 JFK 09:22
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86