Concept

Pattern Matching

Definition

Pattern matching is the operation of testing a value against a structural template and either reporting whether it matches, extracting captured sub-parts, or selecting a code path based on the shape. The pattern is a compact description of a set of acceptable shapes; the match operation is the runtime that decides whether a particular value belongs to that set.

The same idea shows up at every level of the stack. Shell globs match filenames against patterns like *.txt. Regular expressions match arbitrary text against rich patterns with character classes, quantifiers, and capture groups. Language match expressions (Rust, Scala, OCaml, Haskell, Python 3.10+) match data structures against templates and bind variables to their pieces. SQL LIKE clauses match strings against a tiny pattern dialect. The vocabulary differs but the underlying activity — checking a value against a structural template — does not.

Why it matters

How it works

Glob patterns are the simplest dialect. Asterisk matches any number of characters within a single filename component, question mark matches one character, and brackets list character classes. They are designed for filenames, so they treat the path separator specially and do not span directories without help. Regular expressions are the next layer of richness. They add anchors, quantifiers, alternation, grouping, lookaround, and backreferences, and they are the dominant pattern dialect for arbitrary text. Inside this single label hide several non-interchangeable engines — POSIX basic, POSIX extended, Perl-compatible (PCRE), ECMAScript, Vim's flavour — each with subtle syntactic and semantic differences that matter at boundary cases.

Structural pattern matching is the richest form. Instead of pattern-matching characters in a string, it matches values in a data structure: a tuple of a particular arity, a tagged union with a particular variant, a record with particular field names. The match expression in Rust, the case-of in Haskell, the match in Python — all let the pattern itself name the parts of the value that it consumed, so the match is also a destructuring binding. Compilers can typically prove that a structural match is exhaustive, which catches missing cases at compile time rather than at runtime — a robustness gain that text-level regex matching cannot offer.

Where it goes next

Continue exploring

Tags