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.

Reference·40 commands·Last updated 2026-07-21
Back to Reference

Character Class 8

.
Match any single character (except newline unless /s flag)
\d \D
Digit / non-digit, equivalent to [0-9] / [^0-9]
\w \W
Word character / non-word character, equivalent to [a-zA-Z0-9_] / [^a-zA-Z0-9_]
\s \S
Whitespace / non-whitespace (space, Tab, newline)
\b \B
Word 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 \Z
Start / end of string (Python, unaffected by multiline mode)
a|b
Match 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 \2
Backreference 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+\b
Extract all IPv4 addresses from text
"[^"]*"
Match double-quoted strings (no escapes)
^#[0-9a-fA-F]{6}$
Hex color code #RRGGBB

Flag 5

g
Global match (find all matches)
i
Case-insensitive
m
Multiline mode (^ and $ match each line)
s
Single-line mode (. matches newlines)
u
Unicode 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>).

Official References

Commands are compiled from the official docs below. Click to verify the latest usage.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Found an error? Report it

Wrong command or description? Open an issue to help us fix it.

Found an error? Report it