Regex Cheat Sheets

Regex comprehensive cheat sheet

Getting Started

Basic Regex Example

\d{3}-\d{2}-\d{4}

Regex in Code

re.match(r"pattern", string)

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/) { ... }

Regex Resources

Regex101 https://regex101.com/
Regular-Expressions.info https://www.regular-expressions.info/
MDN Regex https://developer.mozilla.org/en-US/doc…
Regexr https://regexr.com/
Pythex https://pythex.org/

Metacharacters

Common Metacharacters

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

Metacharacter Example

^abc$

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

[A-Za-z0-9_]+

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

a{2,4}

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})

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

\bword\b

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

\p{L}+

Substitution & Replace

Substitution Example

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

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

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

/pattern/gi

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}

URL Validation

https?://[\w./-]+

Phone Number

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

Date (YYYY-MM-DD)

\d{4}-\d{2}-\d{2}

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

Debugging Tools

Regex101 Debugger https://regex101.com/
Regexr Debugger https://regexr.com/

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

Platform Docs

Python re https://docs.python.org/3/library/re.ht…
JavaScript RegExp https://developer.mozilla.org/en-US/doc…
Java Pattern https://docs.oracle.com/javase/8/docs/a…

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

Further Reading

Regex101 https://regex101.com/
Regular-Expressions.info https://www.regular-expressions.info/
MDN Regex https://developer.mozilla.org/en-US/doc…
Regexr https://regexr.com/