Skip to content
Part IA Michaelmas, Lent Term

Regular Expression Matching

The Matching Relation

Definition

The matching relation (w,R)(w, R) means “string ww matches regex RR.”

Equivalently: w∈L(R)w \in L(R).

Inductive Definition of Matching

Atoms

(ε,Null)(a,Syma)\frac{}{(\varepsilon, \text{Null})} \qquad \frac{}{(a, \text{Sym}_a)}

  • The empty string matches ε\varepsilon
  • The symbol aa matches the regex aa

Note: No string matches ∅\emptyset (never).

Union

(u,R)(u,Union(R,S))(v,S)(v,Union(R,S))\frac{(u, R)}{(u, \text{Union}(R, S))} \qquad \frac{(v, S)}{(v, \text{Union}(R, S))}

String uu matches R∣SR|S if uu matches RR OR uu matches SS.

Concatenation

(u,R)(v,S)(uv,Concat(R,S))\frac{(u, R) \quad (v, S)}{(uv, \text{Concat}(R, S))}

String uvuv matches RSRS if uu matches RR and vv matches SS.

Star

(ε,Star(R))(u,R)(v,Star(R))(uv,Star(R))\frac{}{(\varepsilon, \text{Star}(R))} \qquad \frac{(u, R) \quad (v, \text{Star}(R))}{(uv, \text{Star}(R))}

The empty string matches R∗R^*, and if uu matches RR and vv matches R∗R^*, then uvuv matches R∗R^*.

Example Derivations

Matching abab against abab

      (ab, Concat(a,b))
         /          \
    (a, a)        (b, b)

Matching abab against a∗b∗a^*b^*

One possible derivation:

    (ab, Concat(Star(a), Star(b)))
           /                    \
   (a, Star(a))            (b, Star(b))
      /     \                 /     \
(a, a)  (ε, Star(a))    (b, b)  (ε, Star(b))

Matching aaabaaab against a∗ba^*b

      (aaab, Concat(Star(a), b))
            /              \
    (aaa, Star(a))      (b, b)
        /      \
   (aa, Star(a))  (a, a)
      /      \
(a, Star(a))  (a, a)
    |
(ε, Star(a))

Proving Properties by Rule Induction

Example: Star Matches Repetitions

Claim: ww matches R∗R^* iff w=w1w2⋯wnw = w_1 w_2 \cdots w_n where each wiw_i matches RR.

Proof:

(⇒)(\Rightarrow) Rule induction on matching derivation for (w,R∗)(w, R^*):

  • Base case: w=εw = \varepsilon = empty concatenation of RR-matches.
  • Inductive case: (uv,R∗)(uv, R^*) from (u,R)(u, R) and (v,R∗)(v, R^*). By IH, v=v1⋯vmv = v_1 \cdots v_m with each vjv_j matching RR. So uv=u⋅v1⋯vmuv = u \cdot v_1 \cdots v_m where uu and each vjv_j match RR.

(⇐)(\Leftarrow) By induction on nn: if w=w1⋯wnw = w_1 \cdots w_n with each wiw_i matching RR:

  • n=0n = 0: w=εw = \varepsilon matches R∗R^* by axiom.
  • n>0n > 0: w=w1⋯wn−1⋅wnw = w_1 \cdots w_{n-1} \cdot w_n matches R∗R^* by concatenating matches.

Non-Matching

Establishing Non-Matching

To prove ww does NOT match RR:

  1. Enumerate all possible derivations
  2. Show none succeed
  3. Or use meta-theorems about L(R)L(R)

Example: baba does not match abab

baba would need to match a∣ba|b or concatenation of matches.

  • Not ε\varepsilon, not a single symbol matching a∣ba|b
  • For concatenation: would need split ba=uvba = uv with uu matching aa and vv matching bb
    • u=bu = b: doesn’t match aa
    • u=bau = ba: v=εv = \varepsilon doesn’t match bb

No derivation exists.

Complexity of Matching

Decision Problem

Given ww and RR, does w∈L(R)w \in L(R)?

Algorithms

  • Backtracking: Try all ways to match; exponential worst case
  • Thompson’s algorithm: Convert to NFA, simulate in O(∣w∣⋅∣R∣)O(|w| \cdot |R|)
  • Glushkov’s algorithm: Direct NFA construction
  • DFA simulation: After NFA-to-DFA, O(∣w∣)O(|w|) matching

Summary

  • Matching is defined inductively by rules
  • Union: match either branch
  • Concat: split and match both parts
  • Star: match zero or more repetitions
  • Non-matching established by showing no derivation exists
  • Efficient algorithms exist for regex matching