Regular Expressions Cheatsheet - Regex Syntax Reference
Essential regex syntax at your fingertips. Organized by functionality from metacharacters to assertions. Reference directly when writing regex patterns.
Back to ReferenceCharacter Class 8
.Match any single character (except newline unless /s flag)
\d \DDigit / non-digit, equivalent to [0-9] / [^0-9]
\w \WWord character / non-word character, equivalent to [a-zA-Z0-9_] / [^a-zA-Z0-9_]
\s \SWhitespace / non-whitespace (space, Tab, newline)
\b \BWord boundary / non-word boundary (zero-width)
[abc]Match any one of a, b, or c
[^abc]Match any character except a, b, c
[a-z]Match character in range a to z
Quantifier 7
*Match 0 or more (greedy)
+Match 1 or more (greedy)
?Match 0 or 1 (optional)
{n}Match exactly n times
{n,}Match at least n times
{n,m}Match n to m times
*? +? ??Lazy (non-greedy) match, match as few as possible
Anchor & Alternation 5
^Start of line (start of each line in multiline mode)
$End of line
\A \ZStart / end of string (Python, unaffected by multiline mode)
a|bMatch a or b
(abc|xyz)Branch in group, match abc or xyz
Group & Capture 8
(pattern)Capturing group, backreference with \1
(?:pattern)Non-capturing group, does not save match
(?P<name>pattern)Named capture group (Python), JS uses (?<name>pattern)
\1 \2Backreference to the nth capture group
(?=pattern)Positive lookahead (must be followed by pattern)
(?!pattern)Negative lookahead (must not be followed by pattern)
(?<=pattern)Positive lookbehind (must be preceded by pattern)
(?<!pattern)Negative lookbehind (must not be preceded by pattern)
Common Patterns 7
^\d{4}-\d{2}-\d{2}$Date format YYYY-MM-DD
^\d{1,3}(\.\d{1,3}){3}$IPv4 address (simplified, no range validation)
^[\w.-]+@[\w.-]+\.\w+$Email address (simplified)
^https?://[\w.-]+HTTP/HTTPS URL start
\b\d+\.\d+\.\d+\.\d+\bExtract all IPv4 addresses from text
"[^"]*"Match double-quoted strings (no escapes)
^#[0-9a-fA-F]{6}$Hex color code #RRGGBB
Flag 5
gGlobal match (find all matches)
iCase-insensitive
mMultiline mode (^ and $ match each line)
sSingle-line mode (. matches newlines)
uUnicode mode (properly handles CJK etc.)
💡 Tips
- Greedy .* matches as much as possible, causing over-capture. Use .*? for lazy matching.
- grep defaults to BRE (Basic Regex) — +, ?, | need escaping or use grep -E (ERE).
- Named capture group syntax differs: Python (?P<name>), JavaScript/Java (?<name>), PHP (?P<name>).