Regex Cheatsheet for Beginners (With Copy-Paste Examples)

The regular expression syntax you will actually use: character classes, quantifiers, anchors, groups, and lookaheads, plus tested patterns for dates, colors, and more.

2026-09-26 · 3 min readTry the Regex Tester →

Regular expressions look cryptic at first, but a small set of building blocks covers most real-world tasks: validating input, searching logs, and doing bulk find-and-replace. Learn these pieces and you can read and write most patterns you'll meet.

The basic building blocks

  • . matches any single character except a newline.
  • \d matches a digit, \w a word character (letters, digits, underscore), and \s whitespace. The capitals \D, \W, and \S mean the opposite.
  • [abc] matches any one of a, b, or c. [a-z] is a range, and [^abc] matches anything except those.
  • ^ and $ match the start and end of the string (or of each line with the m flag).
  • \b matches a word boundary, which is handy for whole-word matches.
  • Use a backslash to match a special character literally: \. matches a period, \( a parenthesis.

Quantifiers: how many times

  • * means zero or more, + means one or more, and ? means zero or one.
  • {3} means exactly 3, {2,5} means between 2 and 5, and {2,} means 2 or more.
  • Quantifiers are greedy by default and grab as much as they can. Add a ? after one (like .*?) to make it lazy so it matches as little as possible.
<.+>     // greedy: on "<b>hi</b>" matches the whole string
<.+?>    // lazy:   matches "<b>" first

Groups, alternation, and backreferences

(cat|dog)s?                      // cat, cats, dog, dogs
(\d{4})-(\d{2})-(\d{2})         // capture year, month, day
(?<year>\d{4})-(?<month>\d{2})   // named groups
(?:abc)+                         // group without capturing
(\w)\1                           // a repeated character, like "ll" in "hello"

Lookaheads and lookbehinds

Lookarounds check what surrounds a position without including it in the match.

\d+(?= dollars)      // digits followed by " dollars"
\d+(?! dollars)      // digits NOT followed by " dollars"
(?<=\$)\d+           // digits preceded by "$"
(?<!\$)\b\d+          // digits NOT preceded by "$"

Flags

  • g finds all matches instead of stopping at the first.
  • i makes the match case-insensitive.
  • m makes ^ and $ match at line boundaries.
  • s lets . match newlines as well.

Practical patterns you can reuse

  • ISO date (format only): ^\d{4}-\d{2}-\d{2}$
  • Hex color: ^#(?:[0-9a-fA-F]{3}){1,2}$
  • Simple email shape: ^[^\s@]+@[^\s@]+\.[^\s@]+$
  • Trim extra spaces: \s{2,} replaced with a single space
  • Slug: ^[a-z0-9]+(?:-[a-z0-9]+)*$

Common mistakes

  • Forgetting to escape special characters like . and ?, which then match far more than intended.
  • Using greedy .* and swallowing more text than you meant to.
  • Nested quantifiers such as (a+)+ that can cause catastrophic backtracking on certain inputs and freeze your program.
  • Assuming every engine behaves the same; lookbehind and named-group syntax differ between JavaScript, Python, and PCRE.

The fastest way to learn is to test against sample text and watch each match and group light up. Build the pattern one piece at a time.

Frequently asked questions

+What does \d+ mean in regex?

\d matches a single digit and + means one or more, so \d+ matches a run of one or more digits, like 2026.

+What is the difference between greedy and lazy quantifiers?

A greedy quantifier (.*) matches as much as possible; a lazy one (.*?) matches as little as possible. Lazy is often what you want when matching between delimiters.

+How do I match a literal dot or bracket?

Escape it with a backslash: \. matches a period and \[ matches an opening bracket.

+Is regex the right tool to validate an email address?

A simple shape check is fine for a form, but regex cannot prove an address exists. Send a confirmation email for real verification.

Regex Tester

Free, runs in your browser — nothing you enter is uploaded.

Open tool →

More guides