Skip to content
Part IA Michaelmas, Lent Term

Regular Expressions

Definition

Abstract Syntax

Regular expressions over alphabet Σ\Sigma are defined inductively:

ConstructorSyntaxMeaning
Nullε\varepsilonMatches the empty string only
Never∅\emptysetMatches nothing
Symbolaa for a∈Σa \in \SigmaMatches the single symbol aa
Union$R_1R_2$
ConcatR1⋅R2R_1 \cdot R_2 or R1R2R_1 R_2Matches R1R_1 followed by R2R_2
StarR∗R^*Matches zero or more repetitions of RR

Concrete Syntax

The typical concrete syntax uses:

  • | for union (alternation)
  • Juxtaposition for concatenation: ab means aa then bb
  • * for Kleene star
  • () for grouping

Precedence (highest to lowest)

  1. Star (*)
  2. Concatenation (implicit)
  3. Union (|)

The Language of a Regular Expression

Definition

Each regex RR denotes a language L(R)⊆Σ∗L(R) \subseteq \Sigma^*, defined inductively:

Regex RRLanguage L(R)L(R)
ε\varepsilon{ε}\{\varepsilon\}
∅\emptyset∅\emptyset
aa{a}\{a\}
$R_1R_2$
R1R2R_1 R_2L(R1)⋅L(R2)={uv:u∈L(R1),v∈L(R2)}L(R_1) \cdot L(R_2) = \{uv : u \in L(R_1), v \in L(R_2)\}
R∗R^*L(R)∗=⋃n≥0L(R)nL(R)^* = \bigcup_{n \geq 0} L(R)^n

Examples

Single Symbols

  • L(a)={a}L(a) = \{a\}
  • L(ab)={ab}L(ab) = \{ab\}
  • L(a∣b)={a,b}L(a|b) = \{a, b\}

Kleene Star

  • L(a∗)={ε,a,aa,aaa,…}L(a^*) = \{\varepsilon, a, aa, aaa, \ldots\}
  • L((a∣b)∗)=Σ∗L((a|b)^*) = \Sigma^* (all strings over {a,b}\{a, b\})
  • L(∅∗)={ε}L(\emptyset^*) = \{\varepsilon\}

Combinations

  • L(a∗b∗)={anbm:n,m≥0}L(a^*b^*) = \{a^n b^m : n, m \geq 0\}
  • L((ab)∗)={ε,ab,abab,ababab,…}L((ab)^*) = \{\varepsilon, ab, abab, ababab, \ldots\}
  • L(a(b∣c)∗)={a,ab,ac,abb,abc,acb,acc,…}L(a(b|c)^*) = \{a, ab, ac, abb, abc, acb, acc, \ldots\}

Idioms and Patterns

Every String

(a∣b)∗=Σ∗ (when Σ={a,b})(a|b)^* = \Sigma^* \text{ (when } \Sigma = \{a, b\})

Strings Starting with aa

a(a∣b)∗a(a|b)^*

Strings Ending with aa

(a∣b)∗a(a|b)^*a

Strings Containing aa

(a∣b)∗a(a∣b)∗(a|b)^*a(a|b)^*

Even-Length Strings

((a∣b)(a∣b))∗((a|b)(a|b))^*

At Least One aa

(a∣b)∗a(a∣b)∗(a|b)^*a(a|b)^*

Exactly One aa

b∗ab∗b^*ab^*

Algebraic Laws

Union

  • Commutative: R∣S=S∣RR|S = S|R
  • Associative: (R∣S)∣T=R∣(S∣T)(R|S)|T = R|(S|T)
  • Identity: R∣∅=RR|\emptyset = R
  • Idempotent: R∣R=RR|R = R

Concatenation

  • Associative: (RS)T=R(ST)(RS)T = R(ST)
  • Identity: Rε=R=εRR\varepsilon = R = \varepsilon R
  • Zero: R∅=∅=∅RR\emptyset = \emptyset = \emptyset R
  • Distributive over union: R(S∣T)=RS∣RTR(S|T) = RS|RT and (R∣S)T=RT∣ST(R|S)T = RT|ST

Star

  • ε∗=ε\varepsilon^* = \varepsilon
  • ∅∗=ε\emptyset^* = \varepsilon
  • R∗∗=R∗R^{**} = R^*
  • R∗=ε∣RR∗R^* = \varepsilon | RR^*

Limitations

Not all languages are regular. For example:

  • {anbn:n≥0}\{a^n b^n : n \geq 0\} is NOT regular
  • {ww:w∈Σ∗}\{ww : w \in \Sigma^*\} is NOT regular
  • Balanced parentheses are NOT regular

These require more expressive formalisms (context-free grammars, etc.).


Summary

  • Regular expressions are built from atoms (ε\varepsilon, ∅\emptyset, symbols) and constructors (union, concat, star)
  • Each regex denotes a language L(R)L(R)
  • Precedence: star > concat > union
  • Useful for pattern matching, lexical analysis
  • Not all languages are regular