正则表达式的简化?
資深大佬 : ispinfx 7
有工具可以把正则简化(如下面的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)