Regex Cheatsheet
Character Classes
| Pattern | Matches | Example |
|---|---|---|
|
Any character except newline |
|
|
Digit [0-9] |
|
|
Non-digit |
|
|
Word char [a-zA-Z0-9_] |
|
|
Non-word char |
|
|
Whitespace [ \t\n\r\f] |
|
|
Non-whitespace |
|
|
Character set — a, b, or c |
|
|
Negated set — not a, b, or c |
|
|
Range |
|
Anchors
| Pattern | Matches | Example |
|---|---|---|
|
Start of line |
|
|
End of line |
|
|
Word boundary |
|
|
Non-word boundary |
|
Quantifiers
| Pattern | Matches | Notes |
|---|---|---|
|
0 or more (greedy) |
|
|
1 or more (greedy) |
|
|
0 or 1 (optional) |
|
|
Exactly n |
|
|
n or more |
|
|
Between n and m |
|
|
0 or more (lazy) |
Matches as few as possible |
|
1 or more (lazy) |
Matches as few as possible |
Groups and Alternation
| Pattern | Purpose | Example |
|---|---|---|
|
Capture group |
|
|
Non-capturing group |
|
|
Backreference to group 1 |
|
|
Alternation (or) |
|
Lookaround (PCRE)
| Pattern | Purpose | Example |
|---|---|---|
|
Lookahead (followed by) |
|
|
Negative lookahead |
|
|
Lookbehind (preceded by) |
|
|
Negative lookbehind |
|
Common Patterns
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
[\w.+-]+@[\w-]+\.[\w.]+
([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}
\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}
CLI Usage
grep -P '(?<=error:\s)\w+' /var/log/syslog
echo "2026-04-10" | sed 's/\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)/\2\/\3\/\1/'
awk '/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ {print $1}' access.log