Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pattern Syntax

The reference tables. The reader will come back to scan these.

Character Classes

PatternMatches
.Any character except newline
\dDigit (0–9)
\DNon-digit
\wWord character (letter, digit, underscore)
\WNon-word character
\sWhitespace
\SNon-whitespace
[abc]Any of a, b, c
[^abc]Any character NOT a, b, c
[a-z]Any character in range a–z

Quantifiers

PatternMeaning
*Zero or more
+One or more
?Zero or one
{n}Exactly n
{n,}n or more
{n,m}Between n and m
*?, +?, ??Non-greedy (lazy) versions

Anchors and Boundaries

PatternMeaning
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNon-word boundary

Grouping and Alternation

PatternMeaning
a|ba or b
(...)Capture group
(?:...)Non-capturing group
(?<name>...)Named capture group

Complete Examples

;; Match a date: YYYY-MM-DD
(bind date-pattern (regex "^(\\d{4})-(\\d{2})-(\\d{2})$"))

;; Match an email (simple)
(bind email-pattern (regex "^\\S+@\\S+\\.\\S+$"))

;; Match a hex color
(bind hex-color (regex "^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"))