Codrix — Regular Expression Syntax Basics | codrix.org

Codrix — Regular Expression Syntax Basics | codrix.org

The Tokens: Metacharacters and Classes

A lookup table of the core tokens: the symbol, what it matches and a one-line example you can paste into an editor.

A literal character matches only itself. This is the baseline against which every other token is read: when the engine reaches a character with no special meaning, it requires that character at the current position and moves on.

The dot matches any single character except a newline in most engines. It is the most permissive single-character token, and it is the usual reason a pattern matches more than the author intended. Pair it with an anchor or a narrower class when you need a specific character.

Repetition operators apply to the element immediately before them. The star repeats that element zero or more times, the plus one or more times, and the question mark zero or one time. Without parentheses, the operator binds to a single character, so a group is needed to repeat a sequence.

The signature mark of this site, drawn as a plate

On narrow screens, swipe or scroll the plate sideways.

A character class in square brackets matches exactly one character from the listed set. It constrains one position rather than a run, so a class used for a run needs a repetition operator on top of it. Ranges and negated classes with a leading caret extend the set.

Anchors assert a position without consuming input. The caret asserts the start of a line, the dollar sign the end. Because they consume nothing, they can be placed anywhere in the pattern and they reduce candidate positions before any character is read.

Alternation with a vertical bar offers two branches, and parentheses group a sequence so that repetition applies to the whole group. Together these two tokens are what let a pattern describe a choice between structures rather than a single fixed string.

  • Look up each symbol before you use it; two symbols can look similar and behave differently.
  • Check whether the operator binds to one element or to a grouped sequence.
  • Confirm the anchor behaviour in your dialect before you rely on it.

Further reading