A-Level · Theory of Computation

Regular Expressions and Finite State Machines

A regular expression and a finite state machine are not two different tools for similar jobs, they are two different notations for exactly the same thing. Anything a regex can match, some FSM accepts, and vice versa. This page proves that with real, verified examples, not just the assertion.

Section 2

Live regex tester

Standard notation: . any character, * zero or more, + one or more, ? zero or one, | alternation (or), () grouping, [abc] any one of these characters.

Try one

Section 3

The same language, built as an FSM

Pick a pattern below. You'll see its FSM, and every single test string checked against both the regex and the FSM, side by side. If they ever disagree, something would be wrong, they never do.

Verified test strings

Exam tips

  • Every one of these FSMs has a trap state, once you enter it, you can never leave, and it's never an accept state. That's what encodes "this string definitely doesn't match", it isn't a special rule, just an ordinary state with no way out.
  • The three patterns here use genuinely different structures, a simple loop, a two-phase counter, and a lookback for a specific ending, yet all three convert to an FSM the same fundamental way, states plus transitions.
Section 4

Check your understanding