Regex Performance in Go

Why It Doesn't Support Backtracking

Go's regexp package uses a non-backtracking NFA algorithm to ensure linear-time performance and prevent catastrophic hangs.

Go's regexp package does not support backtracking because it uses a Thompson NFA algorithm that guarantees linear-time performance, preventing catastrophic backtracking on complex patterns. This design choice prioritizes predictable execution speed over supporting features like lookaheads or backreferences that require backtracking.

// This pattern works fine (no backtracking needed)
re := regexp.MustCompile(`^[a-z]+$`)

// This pattern fails (lookahead requires backtracking)
// re := regexp.MustCompile(`(?=.*[a-z])`) // Error: invalid regexp