Labs › runtimes

Build Your Own Regex Engine

Build a regex engine from Thompson NFA to DFA subset construction. Then run the ReDoS attack pattern on your engine and java.util.regex — one hangs for 30 seconds, the other finishes in 0.01ms. Understand why Java's regex is dangerous in public-facing services.

rust 1.83 advanced ~8h build · ~2h read ~500 LoC (v3)
v0: Thompson NFA construction from regex ASTv1: NFA simulation — O(M*N) guaranteedv2: NFA to DFA — fast repeated matching and ReDoS immunity
☕ Java/Spring integration included jvm perspective

Why this lab exists

Every language runtime you use has a regex engine. Python’s re, Java’s java.util.regex, Ruby’s built-in, JavaScript’s /pattern/g — all of them have a dark secret: they can be made to hang for minutes (or hours) on carefully crafted input. This is called ReDoS — Regular Expression Denial of Service — and it has taken down production systems at Cloudflare, Stack Overflow, and others.

The cause is always the same: these engines use backtracking. When a match fails, they go back and try a different path. For certain patterns and inputs, the number of paths to try is exponential. The alternative — the correct alternative — was published by Ken Thompson in 1968. It runs all paths simultaneously and is guaranteed O(M*N) time, where M is the pattern length and N is the text length. No exceptions. No exponential blowup. Ever.

You’ve used regex engines your whole career. You’ve never had to think about what’s inside one. This lab fixes that. In three stages, you build a working regex engine from first principles — parser, NFA simulator, DFA optimizer — and then you demonstrate the ReDoS attack against a backtracking engine while your own engine sits there unimpressed, finishing in nanoseconds.

The reference for everything in this lab is Russ Cox’s 2007 article “Regular Expression Matching Can Be Simple And Fast.” Read it after (or during) building this lab. It is one of the clearest pieces of technical writing in computer science.

What we’re building

A regex engine in Rust, built in three stages.

We build:

  • A recursive descent parser that converts a regex string into an AST
  • A Thompson NFA compiler that converts the AST into NFA states (O(M) states for pattern length M)
  • An NFA simulator that runs all active states simultaneously (O(M*N) time)
  • Character classes ([a-z], [0-9], [^abc]), ., *, +, ?, |, (), ^, $, \d, \w, \s
  • NFA-to-DFA conversion via subset construction (pre-compute transitions for all 128 ASCII characters)
  • A ReDoS immunity demonstration

We explicitly skip (and why):

  • Capture groups — require tracking per-path group boundaries; orthogonal to the core lesson
  • Unicode beyond ASCII — the Rust regex crate uses Aho-Corasick and Unicode block tables; that’s a full lab on its own
  • Lookahead/lookbehind — zero-width assertions require a different simulation model
  • Lazy quantifiers — greedy vs lazy requires path ordering in the simulation; we just report match/no-match
  • Backreferences — provably not regular; requires backtracking

Mental model

Think of an NFA as a maze with forks. You stand at the entrance. At each fork, instead of choosing one path and backtracking if it’s wrong, you split into clones — one clone per fork. All clones advance simultaneously. The maze accepts if any clone reaches the exit.

For the pattern a*b matching “aaab”:

Initial: {S0}  (start state — epsilon-closure includes S1 and S_star)

After 'a': clones follow the 'a*' loop. Active set grows.
After 'a': same.
After 'a': same.
After 'b': a clone was waiting at S_b. It matches 'b', advances to S_match.
Result: S_match is in the active set. MATCH.

There is no “try path A, fail, try path B.” All paths run in parallel. The active set never exceeds M states (the number of NFA states), no matter how many times a * or + loops.

The epsilon-closure is the key primitive. From any NFA state, you can follow “free moves” — transitions that don’t consume a character — to reach other states. In our implementation, Split states represent these free moves. The epsilon-closure of a set of states is the set of all states reachable without consuming any input.

epsilon_closure({S}) = {
  S,
  all states reachable from S via Split transitions (recursively)
}

This is computed with a depth-first search. It’s O(M) per step.

Prerequisites

None for this lab. It’s designed to be self-contained. A basic familiarity with Rust syntax helps (the code uses Vec, HashMap, enum, and recursion). No regex library knowledge is assumed — we build everything from the grammar up.

Stage-by-stage build

Stage 0 Thompson NFA construction from regex AST ~120 LoC ›
  • Write a recursive descent parser: parse() converts a pattern string into an Ast enum
  • Write an NFA compiler: compile(ast) converts the Ast into Thompson NFA states
  • Test: parse 'a*b', verify NFA state count is proportional to pattern length
  • Key insight: each operator maps to a small, reusable NFA fragment

Every regex operator maps to a standard NFA fragment — a pair of (start, accept) states with internal transitions. Composing fragments is mechanical:

Literal 'a':    S0 --a--> S1(accept)

Concat 'ab':    S0 --a--> S1 --b--> S2(accept)

Star 'a*':      Split -ε-> S_a --a--> Split   (loop back)
                      -ε-> S_accept            (skip)

Plus 'a+':      S_a --a--> Split -ε-> S_a     (loop back)
                                  -ε-> S_accept

Question 'a?':  Split -ε-> S_a --a--> S_accept
                      -ε-> S_accept            (skip entirely)

Alt 'a|b':      Split -ε-> [a frag]
                      -ε-> [b frag]
                both frags point to S_accept

The Split state is the fundamental building block. It has two epsilon edges — it branches without consuming input. The epsilon-closure of a set of states finds everything reachable via Split transitions.

The recursive compiler follows this structure exactly:

pub enum NfaState {
    Literal { matcher: CharMatcher, next: StateId },
    Split    { next1: StateId, next2: StateId },
    Match,
    Dead,
}

Each compile_ast() call returns a Fragment { start, dangling } where dangling is the list of state outputs that haven’t been wired up yet. After building the whole tree, we patch all dangling outputs to point to the single Match state.

Why one Match state? Thompson’s original construction uses a single accept state per NFA. This simplifies simulation: “has any active state reached Match?” instead of “has any active state reached any one of several accept states.”

The parser is a standard recursive descent:

  • parse_alternation handles | (lowest precedence)
  • parse_concat handles juxtaposition
  • parse_quantified handles *, +, ?
  • parse_atom handles literals, ., [...], (), ^, $, \d, \w, \s

The grammar is unambiguous because operator precedence is encoded in the call hierarchy: quantifiers bind tighter than concat; concat binds tighter than alternation.

Stage 1 NFA simulation — O(M*N) guaranteed ~200 LoC ›
  • Implement epsilon_closure(nfa, states) using DFS over Split states
  • Implement simulate(nfa, text, anchored_start, anchored_end) -> bool
  • Handle anchors: ^ restricts the simulation start position; $ checks acceptance at end
  • Stress test: (a+)+ on 25-char input must return in under 10ms

The simulation algorithm processes one character at a time. At each step, the active set represents all possible positions the NFA could be in after processing the characters seen so far.

active = epsilon_closure(nfa, {start_state})

for each character c in text:
    next = {}
    for each state s in active:
        if s is a Literal state and s.matcher matches c:
            add s.next to next
    active = epsilon_closure(nfa, next)

return Match ∈ active

This is precisely O(MN). In the worst case, every NFA state is active at every step. M states, N characters: MN operations total. The epsilon-closure adds another factor of M per step, giving O(M^2 * N) — still polynomial.

Why is this different from backtracking?

A backtracking engine maintains a single position in the NFA. When it fails, it goes back and tries a different path. For (a+)+ on “aaab”:

  • Try: one group containing all ‘a’s: “aaa”. Fails (leftover ‘b’).
  • Backtrack: try two groups: “a”, “aa”. Fails.
  • Backtrack: try two groups: “aa”, “a”. Fails.
  • Backtrack: try three groups: “a”, “a”, “a”. Fails.
  • … and so on for all 2^(N-1) partitions of N ‘a’s.

Our NFA has no backtracking step. It processes each ‘a’ once, advancing all active states simultaneously. After processing 30 ‘a’s and a ‘b’, it knows immediately that no path reached the Match state. Total work: proportional to 30 * (number of NFA states for (a+)+) = about 30 * 20 = 600 operations.

Anchor handling:

^ means “start the simulation at position 0 only.” Without ^, we try starting at every position in the text (substring match).

$ means “only report a match if the active set contains Match after consuming all characters.” Without $, we report a match as soon as Match appears in the active set at any position.

The anchor detection walks the AST before simulation, so there’s no runtime overhead for non-anchored patterns.

Stage 2 NFA to DFA — fast repeated matching and ReDoS immunity ~180 LoC ›
  • Implement subset construction: build a DFA where each state is a set of NFA states
  • Pre-compute the transition table for all 128 ASCII characters
  • Benchmark: measure NFA vs DFA throughput on 100,000 matches of the same pattern
  • Run redos_demo(30): show nanosecond DFA match vs estimated 30-second backtracker

The NFA simulation re-computes the epsilon-closure on every character. For repeated matching against the same pattern, we can pre-compute a Deterministic Finite Automaton (DFA) that replaces the set operations with simple array lookups.

Subset construction:

Each DFA state is a set of NFA states. We start with the epsilon-closure of the NFA start state. For every DFA state D and every character c, the next DFA state is:

next_dfa(D, c) = epsilon_closure({ s.next | s ∈ D, s is Literal, s matches c })

We compute this for all 128 ASCII characters and store the results in a transition table:

transitions: Vec<[usize; 128]>
// transitions[dfa_state][char_code] = next_dfa_state

After construction, matching is:

fn is_match(&self, text: &str) -> bool {
    let mut state = self.start;
    for c in text.chars() {
        state = self.transitions[state][c as usize];
        if state == DEAD { return false; }
    }
    self.accepting[state]
}

One array index per character. No hash maps, no sets, no recursion. This is why the DFA is roughly 3x faster than the NFA simulation for repeated matches.

Cost of construction: Building the DFA is expensive — it’s exponential in the worst case (the DFA for certain patterns can have 2^M states). In practice, for the patterns in this lab, the DFA has fewer states than the NFA. The cost is paid once and amortized over all future matches.

The ReDoS demonstration:

pub fn redos_demo(n: usize) -> ReDoSResult {
    let pattern = "^(a+)+$";
    let attack: String = "a".repeat(n) + "b";  // e.g. "aaaaaaaaaaaaaaab"

    // NFA simulation: O(M*N)
    let t0 = Instant::now();
    let _ = v1::is_match(pattern, &attack);
    let nfa_elapsed = t0.elapsed();  // microseconds

    // DFA match (after pre-build): O(N)
    let mut re = Regex::new(pattern)?;
    re.build_dfa();
    let t1 = Instant::now();
    let _ = re.is_match_dfa(&attack);
    let dfa_elapsed = t1.elapsed();  // nanoseconds

    // A backtracking engine would explore 2^(n-1) paths:
    let backtracker_paths = 1usize << (n - 1);
    // ...
}

For n=30:

  • Our NFA: ~30 * 20 NFA states = 600 operations. Microseconds.
  • Our DFA: 30 array lookups. Nanoseconds.
  • Backtracking engine: 2^29 = ~500 million paths. About 30 seconds on modern hardware.

This is not a performance optimization. It is an algorithmic correctness property. The Thompson NFA/DFA cannot exhibit ReDoS behavior — not because it’s faster hardware, but because the algorithm has no backtracking step to exploit.

Run it yourself

Run it yourself labs/regex-engine ↗
Prereqs: Rust 1.83+cargo
  1. 1
    Clone and build
    git clone https://github.com/pushkarkumar-dev/system-design-labs
    cd system-design-labs/labs/regex-engine
    cargo build
  2. 2
    Run the demo (NFA simulation + DFA throughput + ReDoS timing)
    cargo run --example demo
  3. 3
    Run all tests
    cargo test
  4. 4
    Run benchmarks
    cargo bench
  5. 5
    Start the interactive REPL
    cargo run --bin regex-repl
Expected output
=== regex-engine demo ===

--- v1: NFA simulation ---
[OK] ^[a-z]+$ ~ "hello" => true
[OK] ^[a-z]+$ ~ "Hello" => false
[OK] ^colou?r$ ~ "color" => true
[OK] ^colou?r$ ~ "colour" => true
[OK] ^(foo|bar)+$ ~ "foobarfoo" => true

--- v2: NFA vs DFA throughput ---
NFA: 100000 iters =>  8.21M matches/sec
DFA construction: 1.8ms  (14 states)
DFA: 100000 iters => 24.7M matches/sec

--- ReDoS immunity demo ---
N=10 | aaaaaaaaab  | NFA:  12μs | DFA:  0.3μs | backtracker paths: ~512
N=20 | aaaaaaaaaaaaaaaaaaab | NFA:  28μs | DFA:  0.5μs | backtracker paths: ~524288
N=25 |                      | NFA:  35μs | DFA:  0.6μs | backtracker paths: ~16M
N=28 |                      | NFA:  40μs | DFA:  0.7μs | backtracker paths: ~134M

A backtracking engine on N=30 would take ~30 seconds.
Our engine: nanoseconds.

Benchmarks

Benchmark results on M2 MacBook Pro, 16GB RAM, Apple Silicon
Metric Result Notes
NFA match throughput (email-like pattern, 16-char input) 8,000,000 matches/sec estimated — O(M*N) simulation
DFA match throughput (same pattern, after construction) 25,000,000 matches/sec estimated — O(N) array lookup
DFA construction time (email pattern, ~30 NFA states) 2 ms estimated — one-time cost
ReDoS attack (a+)+ on 30-char input — backtracking engine 30 seconds estimated — 2^29 paths explored
ReDoS attack (a+)+ on 30-char input — our NFA 0.01 ms estimated — O(M*N) = 30 * 20 ops

Stress tests

Nested quantifier stress test: Run (a+)+ against “a” × N for N = 10, 20, 25, 28, 30. Measure time. It should scale linearly. If it grows exponentially, you have accidentally introduced backtracking somewhere in your simulation.

Long text, short pattern: Run ^[a-z]+$ against a 10KB lowercase string. This exercises the O(N) term: M is small (a few NFA states), N is 10,000 characters. Should complete in under 1ms.

Pattern with many alternations: ^(a|b|c|d|e|f|g|h|i|j)+$ creates a large NFA (many Split states). DFA construction may take a few ms. Verify that DFA state count remains bounded (should be small — all paths collapse to the same DFA states after one step).

Non-ASCII input: Feed a UTF-8 string with emoji or Chinese characters to a pattern like ^[a-z]+$. The engine should return false (no match), not crash. Our character matcher returns false for non-ASCII input gracefully.

Invalid patterns: parse("(unclosed"), parse("a**"), parse("[unclosed"). Each should return an Err(String) describing the problem, not panic.

What the toy misses

What the toy misses Honest gaps vs. production
  • Capture groups Our engine answers 'does this pattern match?' but not 'which part matched?' Capture groups require tracking which NFA paths contributed to each group — this complicates the simultaneous simulation significantly.
  • Unicode support Our character classes only handle ASCII. Unicode regex requires handling multi-byte UTF-8, Unicode categories (\p{Lu} for uppercase letter), and locale-dependent collation. The Rust regex crate uses a 256-entry lookup table per Unicode block.
  • Lookahead and lookbehind Zero-width assertions ((?=...) and (?&lt;!...)) require the engine to look ahead or behind without consuming characters. These are hard to implement in the NFA model and are often done via backtracking, making them ReDoS-vulnerable even in engines that otherwise use NFA.
  • Lazy quantifiers `*?` and `+?` prefer the shortest match. Our NFA tries all paths simultaneously, so there is no concept of greedy vs lazy — we just report whether any path matches. Distinguishing shortest vs longest match requires tracking which NFA paths are currently winning.
  • Backreferences `\1` matches the same text that group 1 matched. This cannot be expressed as a regular language — it requires the engine to remember and compare dynamic text, which NFA/DFA cannot represent. Backreferences force backtracking engines even in NFA implementations.
  • JVM's lack of NFA regex Java's regex engine is PCRE-style backtracking. For patterns like `(a+)+` on long strings, it hangs. The JVM has no built-in defense. The Rust regex crate uses NFA/DFA and is immune. This is why high-traffic web services must guard input regex with timeouts.

Wiring it into a Java/Spring service

☕

Wiring it into a Java/Spring service

jvm perspective Spring Boot 3.3
Maven dependencies
org.springframework.boot:spring-boot-starter-web:3.3.0

The JVM perspective: what java.util.regex actually does

java.util.regex is a backtracking NFA engine. You can see this in the javap output of a compiled Pattern:

// Pattern.compile("(a+)+") builds a graph of Node objects:
//
// Pattern$Branch   -> GroupHead -> Curly (possessive) -> GroupTail -> ...
//                  ^                                              |
//                  |______________ backtrack edge ________________|
//
// The "backtrack edge" is the literal ArrayList<int[]> stack that the
// Pattern.match() method maintains. When a path fails, it pops the stack
// and tries the next alternative.

The compiled Pattern object is a linked list of Node instances. Each node has a match(Matcher, int, CharSequence) method that tries to match at the current position and recurses. If it fails, it returns false and the caller tries the next path. This is clean, simple code. It is also the source of ReDoS.

Part A — The production mistake: compiling in a loop

The most common java.util.regex error in real codebases:

// WRONG: Pattern.compile() rebuilds the NFA graph on every call.
// This is O(M^2) in the pattern length. For M=100, that's 10,000 operations per call.
public boolean validateEmail(String input) {
    return Pattern.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$")
                  .matcher(input)
                  .matches();
}

// CORRECT: Compile once, reuse. Pattern is thread-safe.
// The compiled graph is immutable — the Matcher holds mutable state.
private static final Pattern EMAIL =
    Pattern.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$");

public boolean validateEmail(String input) {
    return EMAIL.matcher(input).matches();
}

In a benchmark with 10,000 iterations, the pre-compiled version is consistently 5–10x faster. The speedup is purely from avoiding re-parsing the pattern on each call.

Part B — Named capture groups

Java’s named group syntax uses (?<name>...) (not (?P<name>...) like Python):

// ISO date with named groups
Pattern date = Pattern.compile("(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})");
Matcher m = date.matcher("2026-05-08");

if (m.find()) {
    String year  = m.group("year");   // "2026"
    String month = m.group("month");  // "05"
    String day   = m.group("day");    // "08"
}

Named groups are compiled into the NFA graph as GroupHead / GroupTail node pairs. The Matcher tracks which group boundaries were last set. This is why capture groups add overhead to the NFA simulation — even for the capture-unaware Thompson NFA, groups require tracking per-path state.

Part C — The ReDoS vulnerability and the standard mitigation

The recommended Java mitigation is a timeout guard using ExecutorService:

@Service
public class RegexService {
    // Use a bounded thread pool — not unlimited — to cap thread exhaustion
    private final ExecutorService executor = Executors.newFixedThreadPool(2);
    private final long timeoutMs;

    public boolean validate(String input, String pattern) {
        final Pattern compiled;
        try {
            // UNICODE_CHARACTER_CLASS: \w matches Unicode word chars, not just ASCII
            compiled = Pattern.compile(pattern, Pattern.UNICODE_CHARACTER_CLASS);
        } catch (PatternSyntaxException e) {
            return false; // invalid pattern — reject
        }

        Future<Boolean> future = executor.submit(
            () -> compiled.matcher(input).matches()
        );

        try {
            return Boolean.TRUE.equals(future.get(timeoutMs, TimeUnit.MILLISECONDS));
        } catch (TimeoutException e) {
            future.cancel(true); // request interrupt (best-effort)
            return false;        // treat timeout as "no match"
        } catch (Exception e) {
            return false;
        }
    }
}

The trade-off: The cancelled thread may continue running in the background until the regex engine reaches an interrupt check point (which is rare — Pattern.match() rarely checks Thread.interrupted()). Under a sustained ReDoS attack, you can accumulate zombie threads. The bounded thread pool (newFixedThreadPool(2)) limits this to 2 background threads, after which new validation requests will block waiting for a thread — which is the correct behavior under attack.

The real fix: Use com.google.re2j:re2j — Google’s Java port of RE2 (the same Thompson NFA engine our Rust lab implements). It provides the same Pattern / Matcher API as java.util.regex, drops in as a replacement, and guarantees O(M*N) time. The cost: no backreferences, no lookahead. For input validation (the #1 use case for ReDoS vulnerability), that’s always an acceptable trade.

application.yml

regex:
  match-timeout-ms: 100  # Reject any pattern that takes longer than 100ms

management:
  endpoint:
    health:
      show-details: always

Run it

# Start the Spring Boot demo
cd labs/regex-engine/java-integration && mvn spring-boot:run

Expected output on startup:

--- Demo 1: Pattern pre-compilation ---
  Compile-in-loop (10000 iters): 312ms
  Pre-compiled    (10000 iters):  31ms
  Speedup: 10.1x

--- Demo 2: Named capture groups ---
  Input: 2024-03-15   => year=2024 month=03 day=15
  Input: invalid      => no match

--- Demo 3: ReDoS with java.util.regex ---
  n= 5 | "aaaaab"   | result=false | time=0ms
  n=10 | "aaaaaaaaab" | result=false | time=2ms
  n=15 | "aaaaaaaaaaaaaab" | result=false | time=63ms

--- Demo 4: Backtracking mitigation ---
  Pattern: "^[a-z]+$"  | Input: "helloworld"  | Safe result: MATCHED
  Pattern: "(a+)+"      | Input: "aaaaaaaaa..."| Safe result: TIMED_OUT (rejected)

What surprised me

Where to go deeper

  • Russ Cox (2007): “Regular Expression Matching Can Be Simple And Fast” — the definitive explanation, with the history of how backtracking engines came to dominate — swtch.com/~rsc/regexp/regexp1.html
  • RE2: Google’s production NFA regex engine, the basis for Go’s regexp package — github.com/google/re2
  • The Rust regex crate: uses multiple engines (NFA, DFA, lazy DFA, literal optimizer) and switches between them based on pattern complexity — docs.rs/regex
  • re2j: Google’s Java port of RE2, drop-in replacement for java.util.regex — github.com/google/re2j
  • OWASP ReDoS: catalog of vulnerable patterns and real-world incidents — owasp.org ReDoS
  • Thompson (1968): “Programming Techniques: Regular Expression Search Algorithm” — the original paper, in CACM — short and still readable
  • Next in runtimes: Bytecode VM (lab 35) — the same “compile AST to bytecode, then execute” pattern used here applies directly to a general bytecode interpreter
  • Pattern in other labs: the LSM-tree KV store (lab 2) uses a similar “compile compaction plan, execute lazily” model; the ideas transfer
  • Interview follow-up: “Why does java.util.regex have ReDoS vulnerability but Go’s regexp does not?” — the answer is Thompson NFA vs backtracking, which you can now explain in detail