Regex Cheat Sheet
Complete regular expression reference with patterns, descriptions, and examples.
Character Classes
.Any character except newlinea.c matches "abc", "a1c"\dDigit (0-9)\d\d matches "42"\DNon-digit\D+ matches "abc"\wWord char (a-z, A-Z, 0-9, _)\w+ matches "hello_123"\WNon-word character\W matches "!"\sWhitespace (space, tab, newline)a\sb matches "a b"\SNon-whitespace\S+ matches "hello"Quantifiers
*Zero or moreab* matches "a", "ab", "abbb"+One or moreab+ matches "ab", "abbb"?Zero or onecolou?r matches "color", "colour"{n}Exactly n times\d{3} matches "123"{n,}n or more times\d{2,} matches "12", "123"{n,m}Between n and m times\d{2,4} matches "12", "1234"Anchors
^Start of string/line^Hello matches "Hello World"$End of string/lineWorld$ matches "Hello World"\bWord boundary\bcat\b matches "cat" not "category"\BNon-word boundary\Bcat matches "concatenate"Groups & Lookaround
(abc)Capturing group(\d+) captures digits(?:abc)Non-capturing group(?:ab)+ matches "abab"(?=abc)Positive lookahead\d(?=px) matches "5" in "5px"(?!abc)Negative lookahead\d(?!px) matches "5" in "5em"(?<=abc)Positive lookbehind(?<=\$)\d+ matches "100" in "$100"(?<!abc)Negative lookbehind(?<!\$)\d+ matches "100" in "€100"Character Sets
[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 to z[A-Za-z] matches letters[0-9]Range: 0 to 9[0-9]+ matches numbers🎯 Common Patterns
Regular Expressions Reference Guide
Regular expressions (regex) are powerful patterns for matching text. They're used in search, validation, parsing, and text manipulation across virtually all programming languages. This cheat sheet provides a comprehensive reference for regex syntax.
From basic character classes to advanced lookaround assertions, this guide covers everything you need to write effective regular expressions. Each pattern includes a description and practical example.
Character Classes
Character classes match specific types of characters. \\d matches digits, \\w matches word characters, \\s matches whitespace. Uppercase versions (\\D, \\W, \\S) match the opposite.
Quantifiers Control Repetition
Quantifiers specify how many times a pattern should match. * means zero or more, + means one or more, ? means optional. Curly braces like {n,m} allow precise control over repetition counts.
Anchors and Boundaries
Anchors match positions, not characters. ^ matches start, $ matches end. \\b matches word boundaries, preventing partial matches. These are essential for precise pattern matching.
Groups for Capturing
Parentheses create groups that can capture matched text for later use. Non-capturing groups (?:...) group without capturing. Lookahead and lookbehind assert conditions without consuming characters.