Live data from Hacker News

Finding all regex matches has always been O(n²)

iev.ee

41–50 of 82 posts

Re: Finding all regex matches has always been O(n²)

#41
post #39

FWIW, nim-regex does achieve linear time in the rebar test[0], even if the regex includes capture groups. It's NFA based. [0]: https://github.com/BurntSushi/rebar/pull/20#issuecomment-256...

Oh that is interesting! I haven't even looked Nim regex until now, is it similar to the approach in Go?

Re: Finding all regex matches has always been O(n²)

#43
post #5

Earlier quoted context omitted.

The fact that terms like Aho-Corasick, PLDI, Go, etc. are properly capitalized, including if they begin sentences, but otherwise sentences are uncapitalized, makes me think it's an explicit LLM instruction "don't capitalize the start of sentences" rather than writing style.

ChatGPT also loves Aho-Corasick and seems to overuse it as an optimization fall back idea. ChatGPT has suggested the algorithm to me but the code ended up slowing down a lot.

ChatGPT was heavily RL'd on competitive programming in 2025, and aho-corasick is a traditional algorithm in the competitive programming space.

Re: Finding all regex matches has always been O(n²)

#45
Great stuff.

I would argue that hardened mode should be default though, similar to how siphash is the default hashing function in Rust hash maps. Faster mode should be opt in if the user is confident that the supplied data is nonmalicious and they need the speed up.

Re: Finding all regex matches has always been O(n²)

#46
post #41
post #39

FWIW, nim-regex does achieve linear time in the rebar test[0], even if the regex includes capture groups. It's NFA based. [0]: https://github.com/BurntSushi/rebar/pull/20#issuecomment-256...

Oh that is interesting! I haven't even looked Nim regex until now, is it similar to the approach in Go?

It's similar to RE2, but it lacks the on the fly DFA, ie: it's just the classic Thompson's NFA with some tweaks. It does not implement find all the same way, though.

Re: Finding all regex matches has always been O(n²)

#47
post #45

Great stuff. I would argue that hardened mode should be default though, similar to how siphash is the default hashing function in Rust hash maps. Faster mode should be opt in if the user is confident that the supplied data is nonmalicious and they need the speed up.

I’m still open to it and thinking about it actually. I will explore if it’s possible to eliminate the large losses on common patterns and if it turns out it is then it’s a no brainer.

Going forward this and the extended operators + large pattern perf will hopefully be a strong selling point to gain more traction

Re: Finding all regex matches has always been O(n²)

#48

@ievev Have you ever seen an implementation like @bablr/regex? https://github.com/bablr-lang/regex-vm It's an NFA system so it isn't going to be winning any awards for throughput, but in this particular case it does seem to completely avoid the complexity blowup. It will run your heap out of memory though on really big inputs. The strategy this engine uses is just to evolve the state as a function of time. A match ca…

Do any RE implementations do anything like the query planning that databases do or the rewrites that compilers do that can replace the RE with a different sequence of REs or string searches that might execute faster?

For example in the expression in your example (I'm assuming based on your description of the test data that /d+s+/ means the same as /\d+\s+/ in the RE engines I've used) any match must contain a digit followed by a space.

A scan for all places where a digit is followed by whitespace with each such place then being checked to find the length of the string of whitespace starting there and the length of the string of digits ending there should be linear time and constant heap space.

Re: Finding all regex matches has always been O(n²)

#49
post #35
post #34

I find it weird to have the Perl innovation (?:...) be called "traditional regex". Perl was rather innovative back then, even if it's more than 30 years ago now. Traditional regex is what came before it (grep -E being the most advanced form). I wonder what counts as nontraditional in the author's eyes.

In my head a regex-like thing of Perl origin is known as a 'perlex'.

can call it PCRE now

Re: Finding all regex matches has always been O(n²)

#50
post #48

@ievev Have you ever seen an implementation like @bablr/regex? https://github.com/bablr-lang/regex-vm It's an NFA system so it isn't going to be winning any awards for throughput, but in this particular case it does seem to completely avoid the complexity blowup. It will run your heap out of memory though on really big inputs. The strategy this engine uses is just to evolve the state as a function of time. A match ca…

Do any RE implementations do anything like the query planning that databases do or the rewrites that compilers do that can replace the RE with a different sequence of REs or string searches that might execute faster? For example in the expression in your example (I'm assuming based on your description of the test data that /d+s+/ means the same as /\d+\s+/ in the RE engines I've used) any match must contain a digit f…

You're correct, I accidentally omitted backslashes on \d and \s. I checked and the pattern was correct during the test.
Post reply on HN