Skip to main content
Back to blog

Regular Expressions Cheat Sheet: A Beginner's Guide

A practical cheat sheet covering regex syntax, common patterns, quantifiers, groups, and real-world examples for beginners.


What Are Regular Expressions?

Regular expressions (regex) are patterns used to match character combinations in strings. They are supported in virtually every programming language and many text editors. Once you learn regex, you can search, validate, extract, and replace text with remarkable precision.

Regex can look intimidating at first, but the syntax is logical once you understand the building blocks. This guide covers the essentials you need to start using regex effectively.

Basic Matchers

The simplest regex is a literal string. The pattern hello matches the exact text "hello" in a string. Most characters match themselves literally.

The Dot (.)

The dot matches any single character except a newline. The pattern h.t matches "hat", "hit", "hot", and "h9t".

Character Classes [...]

Square brackets define a set of characters to match at one position. [aeiou] matches any single vowel. [0-9] matches any digit. [A-Za-z] matches any letter.

A caret inside brackets negates the class: [^0-9] matches any character that is not a digit.

Quantifiers

Quantifiers specify how many times the preceding element should repeat.

  • — Zero or more times. a matches "", "a", "aa", "aaa", etc.
  • + — One or more times. a+ matches "a", "aa", "aaa" but not "".
  • ? — Zero or one time. colou?r matches both "color" and "colour".
  • {n} — Exactly n times. a{3} matches "aaa".
  • {n,m} — Between n and m times. a{2,4} matches "aa", "aaa", or "aaaa".
  • {n,} — n or more times. a{2,} matches "aa", "aaa", and so on.

Greedy vs. Lazy

By default, quantifiers are greedy — they match as much text as possible. Adding ? after a quantifier makes it lazy, matching as little as possible. The pattern ".+" applied to "a" "b" matches the entire string "a" "b", while ".+?" matches just "a".

Anchors

Anchors match positions, not characters.

  • ^ — Start of the string (or line, in multiline mode).
  • $ — End of the string (or line).
  • \b — Word boundary. \bcat\b matches "cat" but not "caterpillar" or "concatenate".

Shorthand Character Classes

  • \d — Any digit. Equivalent to [0-9].
  • \D — Any non-digit.
  • \w — Any word character (letters, digits, underscore). Equivalent to [A-Za-z0-9_].
  • \W — Any non-word character.
  • \s — Any whitespace (space, tab, newline).
  • \S — Any non-whitespace character.

Groups and Alternation

Parentheses ( )

Parentheses create capturing groups. They group elements for applying quantifiers and capture the matched text for later use (backreferences or extraction).

(ab)+ matches "ab", "abab", "ababab", etc.

Alternation |

The pipe symbol means "or". cat|dog matches "cat" or "dog". Use parentheses to limit the scope: (cat|dog) food matches "cat food" or "dog food".

Non-Capturing Groups (?: )

If you need grouping but do not need to capture: (?:ab)+ groups "ab" for the quantifier without storing the match.

Common Patterns

Email Address (Simplified)

^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$

This validates that a string looks like an email: one or more valid characters, an @ sign, a domain name, a dot, and a TLD of at least two letters.

Phone Number (US)

^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$

Matches formats like (555) 123-4567, 555-123-4567, and 5551234567.

URL

https?://[\w.-]+(?:\.[\w]{2,})(?:/[\w./?#&=-]*)?

Matches HTTP and HTTPS URLs with various path structures.

IPv4 Address

^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$

Validates that each octet is between 0 and 255.

Flags

Regex engines support flags that modify behavior:

  • g — Global. Find all matches, not just the first.
  • i — Case-insensitive. /hello/i matches "Hello", "HELLO", etc.
  • m — Multiline. ^ and $ match the start and end of each line, not just the whole string.
  • s — Dotall. The dot matches newlines as well.

Test Your Patterns

The best way to learn regex is to practice. Our Regex Tester lets you type a pattern and test string, see matches highlighted in real time, and experiment with flags — all in your browser with no setup required.

Summary

Regular expressions are a powerful tool for text processing. Start with literal matches and character classes, add quantifiers and anchors, and build up to groups and alternation. With practice, patterns that once looked like random symbols become a readable, expressive language for describing text.

Related Tools