ToolPiq.
Guides / Dev

Regex basics: a beginner-friendly cheat sheet

Regular expressions look like line noise, but the core you need day to day is small. Learn these building blocks and you can match, search, and validate text like a pro.

The pieces that matter most

Character classes — what to match

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

Quantifiers — how many

  • * — zero or more. + — one or more. ? — zero or one (optional).
  • {3} — exactly three. {2,5} — between two and five. {2,} — two or more.

Anchors — where

  • ^ — start of the line. $ — end of the line.
  • \b — a word boundary (great for matching whole words).

Putting it together

A few practical patterns:

  • A simple email-ish match: \S+@\S+\.\S+ — some non-space, an @, more non-space, a dot, more non-space.
  • A US-style phone: \d{3}-\d{3}-\d{4} — three digits, dash, three, dash, four.
  • A hex color: #[0-9a-fA-F]{6} — a hash then six hex characters.
  • Trim trailing spaces: \s+$ — whitespace at the end of a line.

Groups and capturing

Wrap part of a pattern in parentheses ( ) to capture it — useful when you want to extract or replace just that piece. For example, (\d{4})-(\d{2})-(\d{2}) captures the year, month, and day of a date separately.

The golden rule: test as you go

Nobody writes a complex regex correctly on the first try. Build it piece by piece against real sample text and watch what matches. A live tester that highlights matches as you type turns regex from guesswork into something you can actually see.

Open the regex tester →


← All guides · Browse all tools →