regex-engine: Building a regex engine from scratch in Rust, without backtracking
Regular expressions are tools we use without questioning how they work. Then comes a day when a pattern like (a+)+$ meets a long input, and an entire service freezes. This crash has a name: catastrophic backtracking, and it’s not a natural law. It stems from one way to implement regex. There’s another, older method based on automata, which works in linear time relative to the input no matter the pattern. Russ explains…
A Rust implementation of a regular expression engine, regex-engine, has been developed from scratch. It uses a four-stage process: parsing, Thompson NFA translation, Pike VM simulation, and lazy DFA construction. The engine supports various regex features, including character classes, escapes, and quantifiers. It was built to demonstrate a linear-time regex engine and to understand the differences between engines that can and cannot guarantee linear time. The implementation includes tests and comparisons with the popular regex crate.
Written by urgent.news from Dev.to's report — not a translation of it. Machine-written — may contain errors; check the original before relying on it.