Skip to content
Regex Cheat Sheet with Examples for Developers

Regex Cheat Sheet with Examples for Developers

What Are Regular Expressions?

Regular expressions (regex) are patterns used to match, search, and manipulate text. They are supported in virtually every programming language and text editor. Whether you are validating email addresses, extracting data from log files, or performing find-and-replace operations, regex is one of the most powerful tools in a developer’s toolkit.

This cheat sheet covers the most commonly used regex syntax with practical examples you can test right away using the devcraft Regex Tester.

Basic Syntax

Literal Characters

The simplest regex is just a literal string. The pattern hello matches the exact text “hello” wherever it appears.

Metacharacters

These characters have special meaning in regex:

  • . — Matches any single character except newline
  • ^ — Matches the start of a string
  • $ — Matches the end of a string
  • \ — Escapes a metacharacter to match it literally

Example: ^Hello matches “Hello world” but not “Say Hello”.

Character Classes

Character classes match one character from a defined set:

  • [abc] — Matches a, b, or c
  • [a-z] — Matches any lowercase letter
  • [A-Z] — Matches any uppercase letter
  • [0-9] — Matches any digit
  • [^abc] — Matches any character except a, b, or c

Shorthand Classes

  • \d — Digit, equivalent to [0-9]
  • \D — Non-digit
  • \w — Word character (letter, digit, or underscore), equivalent to [a-zA-Z0-9_]
  • \W — Non-word character
  • \s — Whitespace (space, tab, newline)
  • \S — Non-whitespace

Example: \d{3}-\d{4} matches phone numbers like “555-1234”.

Quantifiers

Quantifiers specify how many times a pattern should match:

  • * — Zero or more times
  • + — One or more times
  • ? — Zero or one time (optional)
  • {n} — Exactly n times
  • {n,} — At least n times
  • {n,m} — Between n and m times

Example: colou?r matches both “color” and “colour”.

Example: \d{2,4} matches 2 to 4 digits, like “42”, “123”, or “2026”.

Groups and Alternation

Capturing Groups

Parentheses create groups that capture matched text:

  • (abc) — Matches and captures “abc”
  • (a|b) — Matches “a” or “b”

Example: (Mr|Mrs|Ms)\. \w+ matches “Mr. Smith”, “Mrs. Jones”, or “Ms. Chen”.

Non-Capturing Groups

Use (?:...) when you need grouping but do not need to capture:

  • (?:abc) — Groups without capturing

Backreferences

Refer to previously captured groups:

  • \1 — Refers to the first captured group

Example: (\w+)\s+\1 matches repeated words like “the the” or “is is”.

Anchors and Boundaries

  • ^ — Start of string (or line in multiline mode)
  • $ — End of string (or line in multiline mode)
  • \b — Word boundary
  • \B — Non-word boundary

Example: \bcat\b matches “cat” but not “category” or “scattered”.

Lookahead and Lookbehind

These assert what comes before or after a match without including it:

  • (?=...) — Positive lookahead
  • (?!...) — Negative lookahead
  • (?<=...) — Positive lookbehind
  • (?<!...) — Negative lookbehind

Example: \d+(?= dollars) matches “50” in “50 dollars” but not in “50 euros”.

Real-World Examples

Email Validation

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Matches standard email addresses. See the email regex pattern page for variations and edge cases. Note that truly RFC-compliant email validation is extremely complex, but this pattern handles the vast majority of real-world addresses.

URL Matching

https?://[^\s/$.?#].[^\s]*

Matches HTTP and HTTPS URLs in text. For more URL regex variations, see the URL regex reference.

IP Address

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

Matches IPv4 addresses like “192.168.1.1”. Note this does not validate that each octet is 0-255.

Date Format (YYYY-MM-DD)

\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])

Matches ISO date formats with basic month and day validation.

Remove HTML Tags

<[^>]+>

Matches HTML tags for stripping from text. Use a proper HTML parser for complex documents.

Flags

Flags modify how the regex engine processes the pattern:

  • g — Global, find all matches
  • i — Case-insensitive
  • m — Multiline, ^ and $ match line boundaries
  • s — Dotall, . matches newline characters

Test Your Regex

The best way to learn regex is by experimenting. Use the devcraft Regex Tester to write patterns, paste test strings, and see matches highlighted in real time. It supports all standard flags and shows captured groups, making it perfect for building and debugging complex patterns.