HandyBench

Regex Cheat Sheet

Regular expressions (regex) are compact patterns for matching text. They power search-and-replace, validation and data extraction. The syntax looks cryptic at first, but a handful of building blocks covers most real-world needs. Test any pattern here live in the regex tester.

Character classes

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

Quantifiers

  • * — zero or more of the preceding item.
  • + — one or more.
  • ? — zero or one (makes it optional).
  • {3} — exactly 3. {2,5} — between 2 and 5. {2,} — 2 or more.

Anchors and boundaries

  • ^ — start of the string (or line, in multiline mode).
  • $ — end of the string or line.
  • \b — a word boundary, e.g. \bcat\b matches "cat" but not "category".

Groups and alternation

  • (...) — a capturing group; also lets a quantifier apply to several characters.
  • (?:...) — a non-capturing group.
  • a|b — matches a or b ("or").

Ready-to-use patterns

  • Digits only: ^\d+$
  • Simple email: \w+@\w+\.\w+
  • Whole word: \bword\b
  • Strip extra spaces: find \s{2,}, replace with a single space.
  • Trailing whitespace: \s+$

Flags

Flags change how a pattern runs: g finds all matches (not just the first), i ignores case, m makes ^ and $ match per line, and s lets . match line breaks.

Using regex to clean text

Capture groups make replacements powerful: match (\w+), (\w+) and replace with $2 $1 to swap "Last, First" into "First Last". Try patterns against your own text with the find and replace tool, which supports regex and capture groups.