Regex

Regex Cheat Sheets

This Regex cheat sheet includes symbols, ranges, grouping, assertions, and sample patterns

Getting Started

// Basic Regex Example

\d{3}-\d{2}-\d{4}
A simple regex pattern

// Regex in Code

re.match(r"pattern", string)
Python

// Regex in Code

Python
re.match(r"pattern", string)
JavaScript
string.match(/pattern/)
Java
Pattern.compile("pattern")
Grep
grep -E "pattern" file.txt
Ruby
string =~ /pattern/
PHP
preg_match("/pattern/", $string)
Perl
if ($string =~ /pattern/) { ... }

Metacharacters

// Common Metacharacters

.
Any character except newline
^
Start of string
$
End of string
\
Escape special character
|
Alternation (or)

// Metacharacter Example

// Alternation

a|b
Matches a or b
cat|dog
Matches cat or dog

Character Classes

// Character Classes

[abc]
a, b, or c
[^abc]
Not a, b, or c
[a-z]
a to z
[A-Z]
A to Z
[0-9]
0 to 9
[a-zA-Z0-9_]
Alphanumeric or underscore

// Predefined Classes

\d
Digit (0-9)
\D
Non-digit
\w
Word character (a-z, A-Z, 0-9, _)
\W
Non-word character
\s
Whitespace
\S
Non-whitespace

// Character Class Example

Quantifiers

// Quantifiers

*
0 or more
+
1 or more
?
0 or 1
{n}
Exactly n
{n,}
n or more
{n,m}
Between n and m

// Greedy vs Lazy

*
Greedy (as much as possible)
*?
Lazy (as little as possible)
+
Greedy
+?
Lazy

// Quantifier Example

Groups & Capturing

// Grouping & Capturing

(...)
Capture group
(?:...)
Non-capturing group
(?P<name>...)
Named group (Python)
\1, \2, ...
Backreference

// Backreference Examples

(\w+) 
Matches repeated word (e.g. "word word")
(\d{2})-(\d{2})-\2
Matches 12-34-34

// Grouping Example

(\d{3})-(\d{2})-(\d{4})
Using groups and backreferences

Assertions

// Assertions

^
Start of string
$
End of string
\b
Word boundary
(?=...)
Positive lookahead
(?!...)
Negative lookahead
(?<=...)
Positive lookbehind
(?<!...)
Negative lookbehind

// Lookaround Examples

(?<=@)\w+
Word after @
\d+(?=px)
Number before px
(?<!-)foo
foo not preceded by -

// Assertion Example

Unicode & Multiline

// Unicode Classes

\uXXXX
Unicode code point
\p{L}
Any kind of letter (Unicode)
\p{N}
Any kind of numeric character

// Multiline Mode

^
Start of line (with m flag)
$
End of line (with m flag)

// Unicode Example

Substitution & Replace

// Substitution Example

re.sub(r"\\d", "X", "abc123")  # abcXXX
Replace digits with X

// Replacement Patterns

\1
First group
\g<name>
Named group
$1
First group (JS/Java)

// Replace Example

re.sub(r"(\w+) (\w+)", r"\\2 \\1", "hello world")  # world hello
Swap words order

Flags & Modifiers

// Common Flags

i
Ignore case
g
Global search
m
Multiline
s
Dot matches newline
x
Verbose (Python)
u
Unicode (JS)

// Flag Usage

/pattern/gi
JS: global, ignore case
re.I
Python: ignore case
Pattern.CASE_INSENSITIVE
Java: ignore case

// Flag Example

Escaping & Literals

// Escaping Special Characters

// Escapable Characters

\.
Dot
\*
Asterisk
\+
Plus
\?
Question mark
\(
Left parenthesis
\)
Right parenthesis
\[
Left bracket
\]
Right bracket
\{
Left brace
\}
Right brace
\^
Caret
\$
Dollar
\|
Pipe
\/
Slash

// Literal Example

Practical Examples

// Email Validation

[\w.-]+@[\w.-]+\\.[a-zA-Z]{2,6}
Regex for email addresses

// URL Validation

https?://[\w./-]+
Regex for URLs

// Phone Number

\(\d{3}\) \d{3}-\d{4}
US phone number

// Date (YYYY-MM-DD)

\d{4}-\d{2}-\d{2}
ISO date format

Common Mistakes & Debugging

// Common Mistakes

Unescaped metacharacters
Always escape . * + ? ( ) [ ] { } ^ $ | \
Greedy matching
Use lazy quantifiers if needed
Anchors
Remember ^ and $ match start/end of string, not line unless multiline

// Performance Tips

Avoid catastrophic backtracking
Prefer atomic groups or possessive quantifiers
Test on large data
Performance can degrade quickly

Platform Differences & Security

// Platform Differences

Python
Supports named groups, verbose mode
JavaScript
No lookbehind before ES2018
Java
Possessive quantifiers, named groups
PCRE
Very rich feature set

// Security Tips

User input
Never trust user regex input
Denial of Service
Watch for catastrophic backtracking

Best Practices & Tips

// Best Practices

Test patterns
Use online tools to test regex
Use comments
Use verbose mode for complex patterns
Escape properly
Always escape special characters
Keep it simple
Prefer readable patterns
Document
Add comments for complex regex

Land the career of your dreams!

Get inspired, learn, join competitions and grow in your job!

Sign up now