Need to test patterns live? Try the Regex Tester.Open Regex Tool →

Regular Expressions Pocket Guide

A quick reference for Regular Expressions. Write powerful patterns to match, search, and replace text.

Character Classes

Key / CodeDescription
.Any character (except newline)
\dDigit (0-9)
\wWord character (a-z, A-Z, 0-9, _)
\sWhitespace (space, tab, newline)
[abc]Any of a, b, or c
[^abc]Not a, b, or c
[a-z]Character range a to z

Quantifiers

Key / CodeDescription
*0 or more
+1 or more
?0 or 1 (optional)
{3}Exactly 3 times
{2,5}Between 2 and 5 times

Anchors & Groups

Key / CodeDescription
^Start of string/line
$End of string/line
\bWord boundary
(...)Group
(?:...)Non-capturing group

Common Patterns

Key / CodeDescription
Email^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$
ISO Date (YYYY-MM-DD)^\d{4}-\d{2}-\d{2}$
Hex Color^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$

When to Use Regex

Regex is useful for validation, extraction, lightweight parsing, log scanning, and search-and-replace tasks where the input structure is still text. It works best when the pattern is narrow and testable, not when you are trying to fully parse complex nested languages like HTML or SQL with one expression.

Common Regex Mistakes

The most common problem is making patterns too greedy and accidentally matching more text than intended. Another is forgetting to escape metacharacters such as dots and brackets. In production code, it is also easy to forget multiline, unicode, or case-insensitive flags and then misdiagnose the match failure.

Related

Knowledge is power.