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.
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
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 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.
- 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.
- 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.
- 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.
- 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 >.
- 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.
- 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.
Try it yourself
Learn this first
Learn next
- Character encoding (ASCII, Unicode, UTF-8)How text becomes bytes, from ASCII's 128 characters to UTF-8, and why it sometimes garbles. Beginner
- TriesA tree of letters where words that start the same share the same path. Beginner
- Compilers vs interpretersTwo routes from source code to a running program, and the mix most languages use. Beginner
Spotted a mistake on this page?