I don't understand something: the regex expected a space character, followed by the end of the string. If the last character wasn't a space, this could never match. Why did the engine keep backtracking, even though it's easy to figure out that it could never match the regex?
Most simple regular expression evaluators are basically state machines. They hold a few variables: a) what part of the regex am I currently trying to match b) what point in the string am I currently starting at c) how much of the string has this piece of the regex consumed so far Then the state machine basically has three transitions: * if (b+c) terminally matches (a), increment both (a) and (b) and reset (c) * if (b…
(Rust's regex engine does this.)