Regular expressions and the finite-state machines they generate are not powerful enough to handle recursive patterns, such as "n opening parentheses, followed by a statement, followed by n closing parentheses."
The Lex tool and its compiler is designed to generate code for fast lexical analysers based on a formal description of the lexical syntax.