Developer

Regex Basics: A Beginner's Cheat Sheet with Examples

Regular expressions look like keyboard soup, but they're built from a small set of pieces. Learn those pieces and you can search, validate, and extract text in almost any language or editor. Here's the beginner's cheat sheet.

Key takeaways

  • A regex is a pattern that describes text you want to match.
  • Character classes match types of characters; quantifiers set how many.
  • Anchors pin a match to the start or end.
  • Always test a pattern on real samples.

The essential tokens

TokenMatches
.Any single character (except newline)
\d / \DA digit / a non-digit
\w / \WA word char (a–z, 0–9, _) / non-word
\s / \SWhitespace / non-whitespace
[abc]Any one of a, b, or c
[^abc]Any character except a, b, c
[a-z]Any character in the range

Quantifiers (how many)

TokenMeans
*0 or more
+1 or more
?0 or 1 (optional)
{3}Exactly 3
{2,4}Between 2 and 4

Anchors & groups

  • ^ — start of the string   $ — end of the string
  • \b — a word boundary
  • ( ) — a capture group   ( | ) — "or"

Test your pattern live

Type a regex and sample text and see matches highlight instantly.

Open the Regex Tester →

Practical examples

\d{4}                 → a 4-digit year, e.g. 2026
^\d{3}-\d{4}$         → a phone like 555-1234
\b\w+@\w+\.\w+\b      → a simple email shape
#[0-9A-Fa-f]{6}       → a HEX colour like #2D6CDF
\s+                   → one or more spaces (great for cleanup)

Beginner tips

  1. Escape special characters with a backslash to match them literally: \. matches a real dot.
  2. Quantifiers are greedy by default; add ? (e.g. .+?) to make them lazy.
  3. Build incrementally and test after each piece.
  4. Don't parse HTML with regex — use a real parser for nested structures.

Frequently asked questions

What is a regular expression?

A pattern that describes the text you want to find, validate, or extract.

What does \d mean?

Any single digit 0–9. \D matches any non-digit.

Greedy vs lazy?

Quantifiers grab as much as possible by default; add ? to match as little as possible.

Related tools

See our Disclaimer for how to use WorkIQ content and tools.