Codrix — Regular Expression Syntax Basics | codrix.org

Write a pattern, match text

Codrix — Regular Expression Syntax Basics | codrix.org

A regular expression is a sequence of characters that specifies a match pattern in text. You build one by combining literal characters, metacharacters and classes, then test it against lines that should match and lines that should not.

Token

Literal characters

Token

Dot

Token

Repetition operators

Token

Character class

Token

Anchors

Token

Alternation and grouping

Field notes: how to read a pattern

What a regular expression actually is

A regular expression is a sequence of characters that specifies a match pattern in text.

The string you type is read by the engine left to right. Literal characters must appear verbatim; metacharacters instruct the engine to repeat, group, anchor or choose. The engine reports a match at the first position where the whole pattern can be satisfied.

Because the pattern is a specification rather than a search string, the same characters can carry two meanings. The dot matches any character in most engines, but a dot inside a character class or preceded by a backslash behaves differently. Knowing which mode you are in is the first matching rule you need.

  • A literal character matches only itself.
  • A metacharacter modifies how the preceding element is matched.
  • An anchor constrains where the match may start or end.

The token types you will meet first

Look up each symbol in the syntax table before you use it, because two symbols can look similar and behave differently.

A dot stands for any single character except a newline in most engines. The star repeats the preceding element zero or more times, the plus one or more times, and the question mark zero or one time. A character class in square brackets matches one character from the listed set.

Anchors do not consume characters; they assert a position. The caret asserts the start of a line, the dollar sign the end. Alternation with a vertical bar lets the engine try either branch, and grouping with parentheses makes a repeated unit larger than one character.

  • Repetition operators bind to the single element immediately before them unless parentheses group a sequence.
  • A character class matches exactly one character, not a run of them.
  • Anchors fail the match instead of consuming input.

How the engine decides a match

The engine scans candidate positions and accepts the first one where every token in order is satisfied.

Backtracking is the common strategy: when a repetition consumes too much input and a later token fails, the engine gives back one character and tries again. This is why a greedy star can swallow a suffix you meant to keep, and why an anchored pattern can be much faster than an unanchored one.

Anchoring early in the pattern cuts the number of candidate positions. A pattern that must match at the start of a line tests one position per line instead of every position, which is the difference between a search that returns in milliseconds and one that stalls on a large file.

  • Greedy operators take as much as they can, then backtrack.
  • Lazy operators take as little as they can.
  • Anchors reduce candidate positions before any character is consumed.

The token set at a glance

Each card covers one token type with its matching rule and one-line example.

1950sStephen Cole Kleene formalized the concept of a regular language.1968Ken Thompson built Kleene's notation into the editor QED to match patterns in text files, usingjust-in-time compilation.1968Regular expressions entered popular use in pattern matching in a text editor and in lexicalanalysis in a compiler.1986Henry Spencer wrote a regex library that Perl originally derived from.1997Philip Hazel began developing PCRE to closely mimic Perl's regex functionality.
From Kleene to PCRE
Regular expressionMetacharacterCharacter classAnchor
The key terms of this guide, drawn to one scale

Building a first pattern, in order

Write the literal text first, then add one feature at a time and re-test after each change.

Start with the exact substring you want, escape any character that has a special meaning, and confirm the pattern matches. Only then introduce a character class for the parts that vary, and only after that add repetition. Anchor the pattern last, once the body already matches the right lines.

At each step, test against a set of matching lines and a set of non-matching lines. A pattern that matches everything is as wrong as one that matches nothing; the failure modes differ but both mean the same thing: the specification is incomplete.

  • Write the literal text and verify it matches.
  • Escape special characters before they appear in the literal.
  • Add a character class, then repetition, then an anchor.
  • Test against matching and non-matching lines after every change.

Dated milestones

Key dates in the development of regular expression syntax
Year or periodWhat happenedWhy it matters now
1950sStephen Cole Kleene formalized the concept of a regular languageThe theoretical notation the modern tokens descend from
1968Ken Thompson built Kleene's notation into the editor QEDFirst popular use: pattern matching in a text file
1968Regular expressions entered popular use in a text editor and in lexical analysis in a compilerTwo uses that set the shape of modern engines
1980sDifferent syntaxes existed, among them the IEEE POSIX basic and extended standardsA parallel standard family still found in system tools
1986Henry Spencer wrote a regex library that Perl originally derived fromThe origin of the Perl-style token set
1997Philip Hazel began developing PCRE, Perl Compatible Regular ExpressionsA widely distributed attempt to mimic Perl's functionality
Key dates in the development of regular expression syntax

Sources

The list below collects the documented milestones cited on this page.