NFA to Regex Examples: Worked Conversions and What Survives the Trip
Worked examples of converting an NFA to a regular expression by state elimination, with checks, readability tradeoffs and tips for using the result in a regex engine.
Converting a nondeterministic finite automaton (NFA) into a regular expression is a standard exercise in automata theory and a practical step when you want to test a state-machine design in a regex tool. This review walks through several small conversions, explains what the transformation preserves and what it changes, and covers the practical limits of pasting the result into a real regex engine.
Is converting an NFA to a regex suitable for your purpose?
The conversion is worth doing when you need a compact, portable description of a regular language: checking homework, feeding a pattern into a regex tester, or documenting what a small state machine accepts. The main benefit is that one expression captures the whole language and can be run directly by most regex engines. The decisive tradeoff is readability. The expression is guaranteed to describe the same language, but it can grow much faster than the automaton, and different elimination orders produce different-looking, equivalent results.
Keep the automaton, or a plain transition table, as the primary artifact when the machine has more than a handful of states, when people need to review or edit the logic, or when you need to reason about individual states. In those cases the regex is better treated as a derived output than as the source of truth.
What the transformation actually changes
Converting an NFA to a regex changes the representation, not the language. The set of accepted strings stays identical; what disappears is the explicit state structure. This is a structural rewrite of the automaton, not a character-level operation such as reversing text, mirroring glyphs or substituting Unicode characters, so none of those concerns apply here.
The most common hand method is state elimination. First add a new start state with an ε-transition to the old start state, and a new single accepting state that every old accepting state reaches by ε. Then remove the original states one at a time. When you remove a state k, every path that went from some state i through k to some state j is replaced by a direct edge labeled with an expression. When only the new start and accepting states remain, the label between them is the answer.
Elimination rule for removing state k:
R(i,j) := R(i,j) | R(i,k) (R(k,k))* R(k,j)
R(i,k) label from i into k
R(k,k) self-loop on k (use ε if none)
R(k,j) label from k out to j
R(i,j) any existing direct label from i to j (omit if none)
Notation varies between sources. Many textbooks write union as + rather than |, and use ε for the empty string and ∅ for the empty language. The examples below use | for union so they read the same way as most regex engines.
Example 1: strings ending in ab
Alphabet {a, b}. The NFA guesses when the final ab begins.
| From | Input | To |
| q0 (start) | a | q0 |
| q0 (start) | b | q0 |
| q0 (start) | a | q1 |
| q1 | b | q2 (accepting) |
- Add S with S -ε-> q0, and F with q2 -ε-> F.
- Eliminate q1: the path q0 -a-> q1 -b-> q2 becomes q0 -ab-> q2.
- Eliminate q2: q0 -ab-> q2 -ε-> F becomes q0 -ab-> F.
- Eliminate q0: it has a self-loop (a|b), so S -> F is (a|b)*ab.
(a|b)*ab
Check by tracing: aab and bab are accepted because each ends in ab; ba and abb are rejected. The expression matches the intended language.
Example 2: an even number of a's
Alphabet {a}. Two states toggle on each a, and the start state is also accepting, so the empty string is included.
| From | Input | To |
| q0 (start, accepting) | a | q1 |
| q1 | a | q0 |
- Add S -ε-> q0 and q0 -ε-> F.
- Eliminate q1: q0 -a-> q1 -a-> q0 becomes a self-loop aa on q0.
- Eliminate q0: S -> F is (aa)*.
(aa)*
Example 3: even a's with free b's, and why order matters
Alphabet {a, b}. Accept strings with an even number of a's; b's can appear anywhere.
| From | Input | To |
| q0 (start, accepting) | b | q0 |
| q0 (start, accepting) | a | q1 |
| q1 | b | q1 |
| q1 | a | q0 |
Eliminating q1 first: the path q0 -a-> q1 (loop b) -a-> q0 becomes an extra self-loop ab*a on q0, which joins the existing loop b. Eliminating q0 then gives a short result.
(b|ab*a)*
Eliminating q0 first instead: S reaches F through b*, S reaches q1 through ba, q1 reaches F through ab, and q1 gains the loop b|ab*a. Eliminating q1 then produces a longer expression.
b*|b*a(b|ab*a)*ab*
Both expressions describe the same language. The second is correct but harder to read, which illustrates the main practical cost of the conversion: the output depends on elimination order, and a poor order can bury a simple idea. A common heuristic is to eliminate states with few incoming and outgoing edges first. In general, the expression produced from an automaton can be exponentially larger than the automaton itself, so simplification afterwards is often worthwhile.
Example 4: an NFA with an ε-transition
| From | Input | To |
| q0 (start) | a | q1 (accepting) |
| q1 (accepting) | ε | q0 |
- Add S -ε-> q0 and q1 -ε-> F.
- Eliminate q1: q0 -a-> q1 -ε-> q0 becomes a self-loop a on q0, and q0 -a-> q1 -ε-> F becomes q0 -a-> F.
- Eliminate q0: S -> F is a*a.
a*a (equivalent to a+)
ε-transitions need no special preprocessing in state elimination; they simply become ε labels that disappear when concatenated. You can also remove them first with the standard ε-closure construction if you prefer a cleaner automaton to start from.
Will the result stay usable after copying it into a regex tool?
The language survives, but the notation often needs adjustment before a regex engine will treat it the way a textbook does. Points to check:
- Union symbol: textbook + means union, while in most regex engines + means one or more repetitions. Replace textbook + with | before pasting.
- ε and ∅: these symbols are not regex syntax. Drop ε from concatenations, write an optional part with ? or an empty alternative, and remove ∅ branches entirely.
- Full-string matching: theory treats a regex as describing whole strings, but many engines search for a match anywhere in the input. Wrap the expression, for example ^(?:...)$, or use the engine's full-match function.
- Grouping: plain parentheses usually create capture groups. Non-capturing groups (?:...) avoid side effects where the engine supports them.
- Character classes: (a|b) can usually be written [ab], which is shorter and easier to scan.
Theory form: (a+b)*ab
Engine form: ^(?:[ab])*ab$
Testing a pasted expression against sample strings in a regex tester is a useful sanity check, but passing a finite set of examples does not prove equivalence to the NFA. For a definite answer, convert both sides to minimal DFAs and compare them, or use a tool that checks language equivalence directly.
For readability and accessibility, keep a plain-text transition table alongside any state diagram. Diagrams shared as images are not readable by screen readers unless described in text, and a table can be searched, diffed and edited. Long generated expressions are also hard for anyone to follow aloud or by eye, so present a simplified form, or the automaton itself, when the audience needs to understand the logic rather than just run it.
Gaps in this review
No evidence was supplied about specific NFA-to-regex converter tools or regex testers, so this article does not evaluate any particular product's output, notation, accuracy or data handling. The examples above were worked by hand using the standard state elimination method; check the syntax rules of your own regex engine before relying on a pasted result.