Regex Cheat Sheet: Complete Regular Expression Reference

A comprehensive reference guide to regular expression syntax, patterns, and examples for developers.

Advertisement

Introduction to Regular Expressions

Regular expressions (regex or regexp) are one of the most powerful tools in a developer's toolkit. They provide a concise and flexible way to search, match, and manipulate text based on patterns. Whether you're validating user input, parsing log files, extracting data from strings, or performing complex find-and-replace operations, regex is the go-to solution.

Regex was first formalized by mathematician Stephen Kleene in the 1950s as part of formal language theory. Today, regex is implemented in virtually every programming language (JavaScript, Python, Java, C#, Go, Rust, PHP, Ruby) and is built into tools like grep, sed, awk, and most text editors.

While regex can seem intimidating at first, understanding the core concepts makes it much more approachable. This cheat sheet covers everything from basic character matching to advanced lookaheads and backreferences.

Basic Character Matching

PatternDescriptionExample
.Any character except newlineh.t matches hat, hot, hit
\dAny digit (0-9)\d+ matches 123, 42
\DAny non-digit\D+ matches abc, hello
\wWord character (a-z, A-Z, 0-9, _)\w+ matches hello_world
\WNon-word character\W matches @, #, spaces
\sWhitespace (space, tab, newline)\s+ matches spaces/tabs
\SNon-whitespace\S+ matches any_word
\bWord boundary\bcat\b matches cat not cats

Quantifiers

PatternDescriptionExample
*Zero or moreab*c matches ac, abc, abbc
+One or moreab+c matches abc, abbc (not ac)
?Zero or one (optional)colou?r matches color, colour
{n} Exactly n times\d{3} matches 123
{n,} n or more times\d{2,} matches 12, 123, 1234
{n,m} Between n and m times\d{2,4} matches 12, 123, 1234

Character Classes & Groups

PatternDescriptionExample
[abc]Any of a, b, or c[aeiou] matches vowels
[^abc]Not a, b, or c[^0-9] matches non-digits
[a-z]Range: a through z[a-zA-Z] matches any letter
(abc)Capture group(\d+)-(\d+) captures both numbers
(?:abc)Non-capturing group(?:http|https):// groups without capturing
a|bAlternation (or)cat|dog matches cat or dog

Anchors & Boundaries

PatternDescription
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNon-word boundary

Lookaheads & Lookbehinds

Lookaheads and lookbehinds (collectively called lookarounds) are zero-width assertions that match a position without consuming characters:

PatternDescriptionExample
(?=...)Positive lookahead\d+(?=px) matches 10 in 10px
(?!...)Negative lookahead\d+(?!px) matches 10 in 10em
(?<=...)Positive lookbehind(?<=\$)\d+ matches 50 in $50
(?<!...)Negative lookbehind(?<!\$)\d+ matches 50 in €50

Common Regex Patterns

EmailMatches most email addresses
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
URLMatches HTTP/HTTPS URLs
https?://[^\s/$.?#].[^\s]*
Phone (US)Matches US phone numbers
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
IPv4 AddressMatches IPv4 addresses
\b(?:\d{1,3}\.){3}\d{1,3}\b
Date (YYYY-MM-DD)Matches ISO date format
\d{4}-\d{2}-\d{2}
Hex ColorMatches hex color codes
#(?:[0-9a-fA-F]{3}){1,2}\b
Strong PasswordMin 8 chars, upper, lower, digit, special
(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}
HTML TagMatches HTML tags
<[^>]+>

Regex Flags

FlagNameDescription
gGlobalFind all matches, not just the first
iCase-insensitiveMatch regardless of case
mMultiline^ and $ match line starts/ends
sDotAll. matches newline characters too
uUnicodeEnable full Unicode matching

Test Your Regex Patterns

Try our free regex tester with real-time match highlighting, capture group display, and a built-in pattern library.

Open Regex Tester →
Advertisement