3 ms·
I wondered about this for some time. Simple regex (as in formal language theory) are matched in O(n) time by finite automaton. Extended regex like PCRE are mo
by tibiapejagala 10y ago
I wondered about this for some time.
Simple regex (as in formal language theory) are matched in O(n) time by finite automaton.
Extended regex like PCRE are more powerful, but most of the time are implemented by backtracking engines, where really bad regex pattern might go exponential, but even simple pattern as in postmortem can go O(n^2).
Do implementations optimize simple regex patterns to O(n) matching? Even I wrote x86 JIT regex compiler for fun some time ago. Compilation time was really bad, but matching was O(n).
- alexchamberlain 10y agoThere are a few implementations that are linear, but compilation time is then exponential instead.
- tibiapejagala 10y agoWhich is still a big win because * regex pattern is controlled by site, while regex input is external * regex pattern is compiled once, while it is being run for every input