InTab

Blog

Regular expressions: a practical cheat sheet

By Uxair Ijaz · 2026-10-22 · 2 min read

Regular expressions look cryptic, but a small set of pieces covers most real work. Keep this page open next to the regex tester and try each pattern as you read.

Characters

Pattern Matches
. any character except a newline
\d \w \s a digit, a word character (letter, digit, _), whitespace
\D \W \S anything except those
[abc] one of a, b or c
[^abc] any character except a, b or c
[a-z0-9] a range
\. a literal dot (escape special characters with \)

How many

Pattern Means
* 0 or more
+ 1 or more
? 0 or 1 (optional)
{3} exactly 3
{2,5} 2 to 5
*? +? as few as possible (lazy)

Quantifiers are greedy by default: <.+> on <b>bold</b> matches the whole string. <.+?> matches just <b>.

Where

Pattern Means
^ $ start and end of the text (or of each line with the m flag)
\b a word boundary
(?=…) (?!…) followed by / not followed by (lookahead)
(?<=…) (?<!…) preceded by / not preceded by (lookbehind)

Groups

Pattern Means
(…) capture group, reused as $1 in replacements
(?<year>…) named group, lt;year>
(?:…) group without capturing
a|b a or b

Flags

g find all matches · i ignore case · m ^ and $ per line · s dot matches newlines · u full Unicode

Patterns you can copy

Email (practical)      ^[^\s@]+@[^\s@]+\.[^\s@]{2,}$
URL                    https?://[^\s/$.?#].[^\s]*
ISO date               \b\d{4}-\d{2}-\d{2}\b
Time 24h               \b([01]\d|2[0-3]):[0-5]\d\b
Hex colour             #(?:[0-9a-fA-F]{3}){1,2}\b
Whole word "cat"       \bcat\b
Trim spaces            ^\s+|\s+$
Repeated word          \b(\w+)\s+\1\b

For email, a simple pattern plus a confirmation email beats a "perfect" 6,000-character regex.

Tips

  • Start small and grow the pattern while watching the matches highlight.
  • Anchor with ^…$ when validating a whole value, otherwise \d+ happily matches inside abc123.
  • Beware catastrophic backtracking: nested quantifiers like (a+)+$ can freeze on long inputs.
  • Use named groups in anything you will read again in six months.

Tools in this article

  • Regex Tester: Test regular expressions with live highlighting, groups and replace.