← All guides

Regex Basics: Syntax and Common Patterns

A regular expression is a pattern for matching text. Learn the core syntax — anchors, classes, quantifiers — and ready-to-use patterns for email and more.

A regular expression (regex) is a compact pattern that describes a set of strings to match. For example, \d+ matches one or more digits, and ^abc matches text that starts with "abc".

Core building blocks

  • . — any character; \d a digit; \w a word character; \s whitespace
  • ^ and $ — start and end of the line (anchors)
  • * + ? — zero-or-more, one-or-more, optional (quantifiers)
  • [a-z] — a character class; (…) — a capture group

Handy patterns

  • Digits only: ^\d+$
  • Simple email: ^[^@\s]+@[^@\s]+\.[^@\s]+$
  • Hex color: ^#[0-9a-fA-F]{6}$

Test before you ship

Regex is easy to get subtly wrong, so test against real input. The regex tester highlights matches live as you type. For number formats, see binary, decimal and hex.

Explore Tool Suite