Regex Cheat Sheet
A printable regular expressions reference covering character classes, quantifiers, anchors, groups, and common patterns. Essential for any developer.
Regular Expressions Reference
printablepolly.com
Character Classes
.Any character (except newline)a.c → abc, a1c\dDigit [0-9]\d{3} → 123\DNon-digit [^0-9]\wWord char [a-zA-Z0-9_]\w+ → hello_1\WNon-word character\sWhitespace [\t\n\r\f ]\SNon-whitespace[abc]Any of a, b, or c[^abc]Not a, b, or c[a-z]Character rangeQuantifiers
*0 or morea* → "", a, aa+1 or morea+ → a, aa, aaa?0 or 1 (optional)colou?r → color, colour{n}Exactly n times\d{4} → 2024{n,}n or more times{n,m}Between n and m times*?Lazy (match minimum)+?Lazy one or moreAnchors & Boundaries
^Start of string/line^Hello → match at start$End of string/lineend$ → match at end\bWord boundary\bcat\b → "cat" not "catch"\BNot a word boundary(?=...)Lookahead\d(?=px) → 5 in "5px"(?!...)Negative lookahead(?<=...)Lookbehind(?<=\$)\d+ → 99 in "$99"(?<!...)Negative lookbehindGroups & Alternation
(abc)Capturing group(\d+) → capture digits(?:abc)Non-capturing group(?P<name>...)Named group\1Backreference to group 1a|bAlternation (or)cat|dog → cat or dogCommon Patterns
[\w.]+@[\w.]+\.\w+Email (basic)\d{3}[-.\s]?\d{3}[-.\s]?\d{4}US Phone numberhttps?://[\w./\-?&#=]+URL\d{4}-\d{2}-\d{2}Date (YYYY-MM-DD)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}IPv4 Address#?[0-9a-fA-F]{6}Hex ColorFlags / Modifiers
gGlobal (all matches)iCase-insensitivemMultiline (^ and $ per line)sDotall (. matches newline)xExtended (ignore whitespace)Related Templates
FAQ
What is the difference between * and + in regex?
The * quantifier matches zero or more occurrences (the preceding element is optional and can repeat). The + quantifier requires at least one occurrence. For example, 'a*' matches empty string, 'a', 'aa', while 'a+' matches 'a', 'aa' but not empty string.
When should I use non-capturing groups?
Use (?:...) when you need grouping for alternation or quantifiers but don't need to reference the matched text later. This is more efficient and keeps your capture group numbering clean. Example: (?:cat|dog)s matches 'cats' or 'dogs' without capturing.
How do I test and debug regular expressions?
Use online tools like regex101.com or regexr.com that provide real-time matching, explanation of patterns, and test strings. Most code editors also support regex in find/replace. Print this cheat sheet for quick offline reference.