Back to Blog
regex 2025-01-25

Regular Expressions Cheat Sheet for Developers

A practical reference guide to regular expressions with common patterns, syntax, and real-world examples.

Regular expressions (regex) are powerful patterns for matching, searching, and manipulating text. This cheat sheet covers the most commonly used regex features with practical examples.

Basic Syntax

PatternDescriptionExample

|---------|-------------|---------|

.Any character except newlinea.c matches "abc", "a1c" \dAny digit (0-9)\d{3} matches "123" \wWord character (a-z, A-Z, 0-9, _)\w+ matches "hello_123" \sWhitespace character\s+ matches spaces, tabs ^Start of string^Hello matches "Hello world" $End of stringworld$ matches "Hello world"

Quantifiers

PatternDescription

|---------|-------------|

*0 or more +1 or more ?0 or 1 (optional) {n}Exactly n times {n,m}Between n and m times {n,}n or more times

Common Patterns

Email Validation

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

URL Matching

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

Phone Number (US)

^\+?1?[-.\s]?\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4}$

IP Address (IPv4)

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

Date (YYYY-MM-DD)

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

Password Strength

At least 8 chars, one uppercase, one lowercase, one digit, one special:

^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[@$!%?&])[A-Za-z\d@$!%?&]{8,}$

Groups and Lookaheads

Capturing Groups

(\d{4})-(\d{2})-(\d{2})

// Captures year, month, day separately

Non-Capturing Groups

(?:https?|ftp)://

// Groups but doesn't capture

Lookahead and Lookbehind

(?=.*\d)      // Positive lookahead: followed by digit

(?!.*\d) // Negative lookahead: NOT followed by digit

(?<=\$)\d+ // Positive lookbehind: preceded by $

(?

Flags

FlagDescription

|------|-------------|

gGlobal (find all matches) iCase insensitive mMultiline sDotall (. matches newline) uUnicode

Performance Tips

1. Be specific: [0-9] is faster than .*\d

2. Avoid catastrophic backtracking: Use atomic groups or possessive quantifiers

3. Anchor when possible: ^pattern$ is faster than unanchored pattern

4. Prefer character classes: [aeiou] over (a|e|i|o|u)

Test your regular expressions with our Regex Tester tool.