import re
import time
value ="aaaaaaaaaaaaaaaaaaaaaaaaaaaaabs"
strange_regex = re.compile("(a+)+s")
start = time.time()
re.match(strange_regex, value)
end = time.time()print(end - start)
不知道大家执行了多久,在我开发机上使用 Python 3.6+(包括 3.10.x)需要耗费20秒以上,即使 CPU ——Apple M1 Pro 的性能已经相当强悍了。
While writing the text editor sam [6] in the early 1980s, Rob Pike wrote a new regular expression implementation, which Dave Presotto extracted into a library that appeared in the Eighth Edition. Pike's implementation incorporated submatch tracking into an efficient NFA simulation but, like the rest of the Eighth Edition source, was not widely distributed. Pike himself did not realize that his technique was anything new. Henry Spencer reimplemented the Eighth Edition library interface from scratch, but using backtracking, and released his implementation into the public domain.It became very widely used, eventually serving as the basis for the slow regular expression implementations mentioned earlier: Perl, PCRE, Python, and so on.