Programming languages / Regular expressions

Regular expressions

How a regex is built from five kinds of piece, how the engine tries it against text character by character, and why one careless pattern can take millions of steps to fail.

Intermediate7 steps~7 min
Text — what is searched, one cell per character
Pattern — the regex, one pill per piece
Match — characters the pattern has claimed
Failure — the character where an attempt breaks
// THE SHORT VERSION

In 30 seconds

A regular expression is a compact pattern for finding or checking text. It is built from a few kinds of piece: plain characters, sets of allowed characters, repeat counts, groups that remember what they matched, and anchors for the start and end.

The engine tries the pattern from left to right. Repeats are greedy by default, taking as much as they can; when the rest of the pattern then fails, the engine backtracks, giving characters back and trying again.

Usually that is quick. A repeat nested inside a repeat can make it explode.

Regular expressions, step by step

STEPS01 / 07

Syntax and behaviour shown are those of JavaScript and other backtracking engines (Python, Java, PCRE). Engines such as RE2 or Rust’s never backtrack, so they cannot blow up, but drop a few features.

STEP 01 / 07 Literals: match as written
THE MATH
/cat/ on "the cat" match at index 4
  1. Step 1: Literals: match as written. A regular expression, or regex, is a pattern that describes text. The simplest pattern is plain characters: cat matches a c, then an a, then a t. The engine lays the pattern against the text at position 0, and each time it fails it moves one place to the right and tries again.
  2. Step 2: Classes: one of a set. A character class stands for a single character chosen from a set. [aou] means “an a, an o or a u”, so c[aou]t matches cat and cot but not cit. Common sets have shorthands: \d is any digit, \w is a letter, digit or underscore, and a dot is any character except a line break.
  3. Step 3: Quantifiers: how many. A quantifier follows a piece of the pattern and says how many times it may repeat. \d alone is one digit; \d+ is one or more digits in a row, so it takes all of 2024. The others are ? for zero or one, * for zero or more, and braces for exact counts.
  4. Step 4: Groups and anchors. Round brackets make a group: the engine remembers what each group matched, so you can pull out the two numbers. Anchors match a position rather than a character: ^ is the start of the text and $ is the end. With both, the whole text must fit the pattern, so “id 10-42” is rejected.
  5. Step 5: Greedy or lazy. By default a quantifier is greedy: it takes as many characters as it can while still letting the rest of the pattern match. Here .+ runs all the way to the last >. Put a ? after the quantifier and it turns lazy, taking as few as possible, so the match stops at the first >.
  6. Step 6: Backtracking. How does the greedy version find its match? .+ first swallows everything to the end of the text, leaving nothing for >. So the engine backs up: .+ gives back one character and > is tried again, and again, until it fits. Undoing a choice to try the next option is called backtracking.
  7. Step 7: Catastrophic backtracking. Put one repeat inside another and the engine can be left with a huge number of equivalent ways to carve up the same characters. Against aaaa followed by b this pattern must fail, but only after trying every way to group the a’s. Each extra a doubles the work, and thirty of them can freeze a program.
VALUES SO FAR
catindex 4literal
[aou]····class
\d+····repeat
group 1····captured
group 2····captured
greedy····match
lazy····match
backs up····backtrack
30 a’s····attempts

Spotted a mistake on this page?