Convert a DFA to a regular expression

How DFA-to-regex conversion works, what it preserves, where the output gets unwieldy, and how to carry a textbook expression into a real regex tester safely.

Turning a deterministic finite automaton (DFA) into a regular expression is a standard step in automata theory and an occasional practical need when a state machine has to be expressed as a pattern for a regex engine. This review covers what the conversion changes, when it is worth doing, and what can go wrong when the result is copied into real tools. It is based on general, verifiable principles of formal language theory. No specific converter or website was tested, so no tool-specific claims are made.

Is the conversion suitable for your use?

The main benefit is a compact, portable description of the same language. A regular expression can be pasted into a regex tester, embedded in a validator, or written into a proof. The decisive tradeoff is size and readability. The resulting expression can be far longer than the automaton, and for some DFAs every equivalent regular expression is exponentially larger than the number of states. Small automata usually convert cleanly. Large ones often produce expressions that are technically correct but impractical to read or maintain.

  • The conversion tends to fit well when:
  • the DFA is small, such as a homework exercise or a short protocol rule
  • you need a pattern for a tool that only accepts regular expressions
  • you want to show that a language is regular, or compare two descriptions of one language
  • Plain text or the automaton itself is often the better choice when:
  • the DFA has many states and dense transitions
  • humans need to review or edit the logic later
  • the target system can run the state machine directly as code or a transition table

What the conversion actually changes

The conversion changes the representation, not the language. The DFA and the resulting expression accept exactly the same set of strings. This equivalence follows from Kleene's theorem, which states that finite automata and regular expressions describe the same class of languages. Nothing about the alphabet, the strings or their order is altered. The terms reverse, mirror and font from text-styling tools do not apply here. If you need the reverse of a language, that is a separate construction and not part of a DFA-to-regex conversion.

Three standard methods are widely taught. They produce equivalent but different-looking results:

  • State elimination: add a fresh start state and a single accepting state, connect them with empty (ε) transitions, then remove the original states one at a time. Each edge is relabeled with an expression that accounts for the paths through the removed state.
  • Algebraic method (Brzozowski): write one equation per state and solve the system with Arden's lemma. If X = AX + B and A does not contain the empty string, then X = A*B.
  • Transitive closure (Kleene, McNaughton–Yamada): build expressions for paths between each pair of states, allowing intermediate states numbered up to k, and increase k step by step. This method is systematic but produces large intermediate expressions.

The order in which states are eliminated, or equations are solved, affects the shape and length of the output but never its meaning. Finding the shortest equivalent expression is computationally hard in general, so practical tools usually return a correct expression rather than a minimal one. Minimizing the DFA first often helps but does not guarantee a short result.

Worked example: an even number of a's

Take a DFA over the alphabet {a, b} with two states. q0 is the start state and the only accepting state. Reading a moves between q0 and q1 in either direction, and reading b keeps the machine in its current state. The DFA accepts exactly the strings that contain an even number of a's.

State | on a | on b | Accepting
q0 (start) | q1 | q0 | yes
q1 | q0 | q1 | no

Eliminating q1 first: q1 is entered from q0 on a, loops on b, and returns to q0 on a. This gives q0 a new self-loop labeled b | ab*a. Eliminating q0 then leaves a single expression:

(b|ab*a)*

Eliminating q0 first instead produces a different expression for the same language:

b*|b*a(b|ab*a)*ab*

Both expressions accept exactly the strings with an even count of a's. The second covers the zero-a case separately and then brackets the remaining a's between a first and last a. This example illustrates why two correct answers to the same exercise can look unrelated. To check that two expressions are equivalent, convert each to a minimal DFA and compare the DFAs. Comparing the expressions as text will not work.

Will the result stay usable after copying or sharing?

Most practical problems appear when an expression written in textbook notation is moved into a programming-language regex engine. The two notations overlap but are not identical:

  • Union: many textbooks write union as + or ∪. In most regex engines + means one or more, so a pasted textbook expression can silently change meaning. Replace union with |.
  • Empty string and empty language: ε and ∅ have no direct syntax in typical engines. An empty group can stand in for ε. A construct that never matches, such as the empty negative lookahead (?!), can stand in for ∅. Check that your engine supports the construct you choose.
  • Anchoring: a formal expression describes whole strings, while many engines search for a match anywhere in the input. Add start and end anchors to test full-string membership.
  • Grouping: plain parentheses capture in most engines. Non-capturing groups avoid side effects and can make long generated patterns cheaper to run.
  • Symbols in the alphabet: if the alphabet includes characters that are special in regex syntax, such as . * ( or |, escape them.

Applied to the example, a full-string pattern for a typical engine looks like this:

^(?:b|ab*a)*$

Checking a few strings by reasoning gives the expected results. abba and bb match, with two and zero a's. aba does not match, because it ends with an unpaired a. Engines differ in details such as how anchors treat line breaks, so test the pattern in the engine where it will actually run.

Copying from documents adds its own risks. Typeset notes may contain look-alike characters such as Greek ε, math operators, or a Unicode asterisk instead of the ASCII *. Characters like these can break a pattern or change its meaning without any visible sign. Retyping the expression in plain ASCII, or checking it in a tester that highlights syntax, catches most of these problems.

For accessibility, long generated expressions are hard to follow for anyone, and state diagrams shared only as images are not readable by screen-reader users. A plain-text transition table, like the one above, together with a one-sentence description of the language gives readers a usable equivalent. No assistive technology was tested for this review, so how specific screen readers announce symbols such as ε, ∅ or * is not established here. If that matters for your audience, test with their tools.

Bottom line

Converting a DFA to a regular expression preserves meaning exactly and works well for small automata, proofs and tools that only accept patterns. The output can grow quickly and rarely reads naturally, and textbook notation must be translated before it works in a real regex engine. When people need to understand or maintain the logic, share a plain-text description or transition table alongside the expression, or instead of it.