Skip to content
ToolDesk

Regular Expression Cheat Sheet — Metacharacters and Patterns (2026)

Updated 2026-09-01

A regular expression (regex) is a pattern notation for finding a "shape" inside text. There are not many symbols to learn, but combinations change the meaning, so keeping a cheat sheet handy makes it far easier. You can paste the examples here straight into our regex tester to see how they behave.

Basic metacharacters

  • . — any single character (except newline)
  • \d — a digit (0-9) / \D — a non-digit
  • \w — word character (letters, digits, underscore) / \W — the opposite
  • \s — whitespace (space, tab, newline) / \S — non-whitespace
  • [abc] — one of a, b, or c / [^abc] — anything else
  • [a-z] — a range (lowercase letters) / [0-9] — digits

Quantifiers (repetition)

  • * — zero or more / + — one or more / ? — zero or one
  • {3} — exactly 3 / {2,5} — 2 to 5 / {2,} — 2 or more
  • Default is "greedy" (as much as possible); add ? for "lazy" (shortest), e.g. .*?

Anchors and groups

  • ^ — start of line / $ — end of line / \b — word boundary
  • ( ) — group and capture / (?: ) — group without capturing
  • a|b — a or b (alternation)
  • \1 — the same text as group 1 (backreference)

Ready-to-use patterns (simplified)

These are handy everyday patterns written for practicality, not to judge every edge case strictly (full email or URL validation is notoriously complex). Always test them against your real input before relying on them.

  • Digits only — ^\d+$
  • Date (YYYY-MM-DD) — ^\d{4}-\d{2}-\d{2}$
  • Time (HH:MM, 24h) — ^([01]\d|2[0-3]):[0-5]\d$
  • Email (simplified) — ^[^@\s]+@[^@\s]+\.[^@\s]+$
  • URL (http/https, simplified) — ^https?://[^\s]+$
  • Alphanumeric only (IDs) — ^[A-Za-z0-9]+$
  • Hex color — ^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$

Key flags

  • g — find all matches (global)
  • i — case-insensitive
  • m — treat ^ and $ as start/end of each line
  • s — let . match newlines too (dotAll)
  • u — Unicode mode (handles emoji and \p{...} correctly)

Easy things to trip on

  • To match a literal . or *, escape it as \. or \*
  • When greedy matching grabs too much, add ? to make it lazy (.* → .*?)
  • Nested repetition like (a+)+ can blow up on failing input (ReDoS); avoid it
  • Flavors differ by language (named-group syntax, lookbehind support, Unicode handling of \d)

The fastest way to build intuition is to try these patterns against real strings in our regex tester. It highlights matches, shows capture-group contents, and flags compatibility notes when moving a pattern to another language.