You have typed a regex — a short string like [A-Z][a-z]+\d{2,4} — into a text editor, a terminal, or a script, and within milliseconds it scanned megabytes of text and flagged every match. Where does that speed come from, and what exactly can a regex match?
The answers go back to 1956, when the mathematician Stephen Kleene defined regular languages — the class of patterns that can be described by a finite set of states and transitions. In 1959, Ken Thompson (who later co-created Unix) showed how to compile any regular expression into a nondeterministic finite automaton (NFA) in linear time and simulate it without ever backtracking. The result: matching always runs in in the length of the input, no matter how complex the pattern.
Regular expressions define exactly the regular languages — not one pattern more, not one less. That tight correspondence between the notation and the machine is what makes them both powerful and predictable.
Comments
Loading comments...