Web Utility Desk
Blog Guides JSON URL Resources Search

Resources ยท Cheat Sheets

Regular Expressions (Regex) Cheat Sheet & Quick Reference

Overview

Regular expressions (Regex) provide concise syntax for searching, matching, and manipulating text strings across log files, input validation forms, and data parsers. Mastering core metacharacters, character classes, quantifiers, and lookaround assertions eliminates brittle string manipulation loops.

This reference sheet organizes standard ECMAScript and PCRE patterns into structured syntax tables for instant lookup during coding and debugging rounds.

Syntax Tables: Character Classes & Anchors

Token Name / Meaning Example Match
`.` Wildcard matching any character except line breaks (unless `s` dotAll flag is active). `a.c` matches `"abc"`, `"a1c"`
`\d` / `\D` Digit character (`[0-9]`) / Non-digit character (`[^0-9]`). `\d\d` matches `"42"`
`\w` / `\W` Word character (`[a-zA-Z0-9_]`) / Non-word character. `\w+` matches `"user_1"`
`\s` / `\S` Whitespace (space, tab `\t`, newline `\n`, carriage return `\r`) / Non-whitespace. `a\sb` matches `"a b"`
`^` / `$` Anchor for start of string (or line with `m` flag) / End of string (or line). `^Error` matches `"Error code 404"`
`\b` / `\B` Word boundary (transition between `\w` and `\W`) / Non-word boundary. `\bcat\b` matches `"cat"` but not `"catalog"`

Quantifiers & Groups

Syntax Quantifier / Group Meaning Example
`*` / `+` / `?` Zero or more / One or more / Zero or one (optional). `colou?r` matches `"color"` or `"colour"`
`{n,m}` Match between `n` and `m` repetitions. `{n}` matches exactly `n` times. `\d{4}` matches `"2026"`
`(...)` / `(?:...)` Capturing group (stores match for extraction or backreferences) / Non-capturing group. `(https?):\/\/(.*)` extracts protocol and host
`(?=...)` / `(?!...)` Positive lookahead (asserts what follows without consuming) / Negative lookahead. `\d+(?=px)` matches `"100"` in `"100px"`

Practical Patterns for Daily Work

Standard patterns frequently required across application input validation and log parsing:

# Email Validation (Pragmatic RFC 5322 approximation)
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

# ISO 8601 Date String (YYYY-MM-DD)
^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$

# Slug / URL Safe Identifier
^[a-z0-9]+(?:-[a-z0-9]+)*$

# IPv4 Address Extraction
\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b

Common Mistakes & Pitfalls

  • Unescaped Metacharacters: Forgetting to escape literal dots (`.`) in domain matching (`example.com` matches `exampleXcom`). Always write `example\.com`.
  • Catastrophic Backtracking: Nested greedy quantifiers like `(a+)+$` on non-matching strings can freeze regex engines. Avoid nested quantifiers on overlapping character sets.
  • Ignoring Anchors: Omitting `^` and `$` during form input validation allows malicious strings embedded within larger payloads to slip through.

Best Practices

  • Use Non-Capturing Groups: Default to `(?:...)` instead of `(...)` when grouping alternatives unless you specifically need to extract the submatch. This improves memory performance.
  • Test Edge Cases: Always verify regex behavior against empty strings, unicode characters, and long inputs using live browser testing utilities.

Related Interactive Tools

RT

Regex Tester

Test regex patterns live against custom sample strings with instant group highlighting.

Open tool
RG

Regex Generator

Generate production-grade regex patterns automatically from plain English requirements.

Open tool

Related Engineering Guides

RX

Regex Cheat Sheet with Examples

Comprehensive article breaking down anchors, character classes, lookarounds, and flags.

Read guide
ERR

Common Regex Mistakes

Learn how to prevent catastrophic backtracking and unescaped wildcard vulnerabilities.

Read guide
GDE

Regex Tester Beginner Guide

Step-by-step walkthrough for building, testing, and debugging regular expressions.

Read guide

Frequently Asked Questions

What is the difference between greedy and lazy quantifiers?

Greedy quantifiers (*, +, {n,}) match as many characters as possible before backtracking. Adding a question mark (*?, +?) makes them lazy, matching the minimum number of characters needed.

Why do regular expressions behave differently across programming languages?

While core ECMAScript and PCRE standards share common metacharacters, engines differ in advanced features like lookbehinds, named capture groups, and unicode property escapes.

How can I test complex regex patterns safely against production logs?

Use our interactive client-side Regex Tester to verify patterns against live sample strings with instant group highlighting and execution time checks.

Web Utility Desk

Free web utility tools for everyday technical work.

Tools JSON Formatter JSON Validator CSV to JSON Converter Regex Tester Regex Generator SQL Formatter JSON Compare Tool HTML Minifier CSS Minifier JavaScript Minifier
Site About Blog Guides Resources Contact Privacy Terms