1. Regex Engines: DFA vs NFA Mechanics

There are two distinct computational models used by regex engines across programming languages:

2. The Anatomy of Catastrophic Backtracking ($O(2^n)$)

Consider the deceptively harmless regex: (a+)+$. When evaluated against the string "aaaaX":

(a+)+$ tested on "aaaaaaaaaaaaaaaaaaaaaaaaaaaa!"
Length 20 string: ~1,000,000 iterations
Length 30 string: ~1,000,000,000 iterations (Browser / Server freezes completely!)

Because the inner quantifier a+ and outer quantifier ()+ overlap, there are $2^{n-1}$ mathematical permutations of dividing $n$ identical characters. When the trailing character fails to match, the engine exhaustively tests every possible permutation before returning false, locking the CPU thread at 100% usage.

Test and Debug Regex In Real-Time

Validate regular expressions, inspect capture groups, and check for backtracking issues in an instant sandbox.

Open Free Regex Tester →