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$
ConcatR1R2R_1 \cdot R_2 or R1R2R_1 R_2Matches R1R_1 followed by R2R_2
StarRR^*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:uL(R1),vL(R2)}L(R_1) \cdot L(R_2) = \{uv : u \in L(R_1), v \in L(R_2)\}
RR^*L(R)=n0L(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(ab)={a,b}L(a|b) = \{a, b\}

Kleene Star

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

Combinations

  • L(ab)={anbm:n,m0}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(bc))={a,ab,ac,abb,abc,acb,acc,}L(a(b|c)^*) = \{a, ab, ac, abb, abc, acb, acc, \ldots\}

Idioms and Patterns

Every String

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

Strings Starting with aa

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

Strings Ending with aa

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

Strings Containing aa

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

Even-Length Strings

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

At Least One aa

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

Exactly One aa

babb^*ab^*

Algebraic Laws

Union

  • Commutative: RS=SRR|S = S|R
  • Associative: (RS)T=R(ST)(R|S)|T = R|(S|T)
  • Identity: R=RR|\emptyset = R
  • Idempotent: RR=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(ST)=RSRTR(S|T) = RS|RT and (RS)T=RTST(R|S)T = RT|ST

Star

  • ε=ε\varepsilon^* = \varepsilon
  • =ε\emptyset^* = \varepsilon
  • R=RR^{**} = R^*
  • R=εRRR^* = \varepsilon | RR^*

Limitations

Not all languages are regular. For example:

  • {anbn:n0}\{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