Regular Expressions & Kleene's Theorem

a regular expression

Imagine you want to describe a whole family of words with one short formula instead of listing them one by one. A regular expression is exactly that: a tiny piece of notation that names a set of strings by saying how to build them. Where a finite automaton is a machine that reads a string and answers yes or no, a regular expression is a recipe that generates the same set of strings. They are two views of the same thing.

A regular expression is built up from a few simple pieces. The base cases are single symbols of the alphabet Σ (Sigma), the empty string ε (epsilon), and the empty set ∅. From those you combine larger expressions with three operations: union (written with + or |, meaning 'this pattern or that one'), concatenation (writing two patterns side by side, meaning 'this then that'), and the Kleene star (written with a single star, meaning 'zero or more copies'). For example, over the alphabet {a, b}, the expression a(a+b)*b describes every string that starts with an a and ends with a b.

Each regular expression denotes a language, that is, a set of strings, defined by reading the expression's structure from the inside out. The remarkable fact (Kleene's theorem) is that the languages you can write this way are exactly the regular languages, the same class recognized by finite automata. A common confusion: the 'regular expressions' in programming languages add extra features like backreferences and lookahead that go beyond this mathematical object, so the engineering tool and the theory tool share a name but are not the same.

Over {0, 1}, the regular expression (0+1)*1 denotes all binary strings that end in 1: 1, 01, 11, 001, 101, and so on. The (0+1)* part means 'any prefix at all', and the trailing 1 forces the last symbol.

One short formula stands for an infinite set of strings.

The mathematical regular expression and the programming 'regex' are different objects: the latter adds non-regular features (backreferences, lookahead) that can describe languages no finite automaton can recognize.

Also called
regexregexp正規式