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: wL(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 RSR|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 RR^*, and if uu matches RR and vv matches RR^*, then uvuv matches RR^*.

Example Derivations

Matching abab against abab

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

Matching abab against aba^*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 aba^*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 RR^* iff w=w1w2wnw = 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=v1vmv = v_1 \cdots v_m with each vjv_j matching RR. So uv=uv1vmuv = u \cdot v_1 \cdots v_m where uu and each vjv_j match RR.

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

  • n=0n = 0: w=εw = \varepsilon matches RR^* by axiom.
  • n>0n > 0: w=w1wn1wnw = w_1 \cdots w_{n-1} \cdot w_n matches RR^* 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 aba|b or concatenation of matches.

  • Not ε\varepsilon, not a single symbol matching aba|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 wL(R)w \in L(R)?

Algorithms

  • Backtracking: Try all ways to match; exponential worst case
  • Thompson’s algorithm: Convert to NFA, simulate in O(wR)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