Regex Cheat Sheet
Regular expression syntax reference plus ready-to-use patterns for grepping IPs, hashes, emails, and secrets.
Overview
Regular expressions describe text patterns. They power grep, sed, awk, Burp, and nearly every search box in a security tool. This sheet covers the core syntax (anchors, classes, quantifiers, groups) and a set of battle-tested patterns for extracting the data you care about from messy output.
Reference material. Use pattern matching against data you are authorized to process.
Anchors & boundaries
Start of line
^End of line
$Word boundary
\bNon-word boundary
\BCharacter classes
Any character (except newline)
.Digit / non-digit
\d \DWord char [A-Za-z0-9_] / non-word
\w \WWhitespace / non-whitespace
\s \SAny of a,b,c / none of them
[abc] [^abc]Ranges inside a class
[a-z0-9]Quantifiers
0 or more
*1 or more
+0 or 1 (optional)
?Exactly 3 / 2 to 4 / 2 or more
{3} {2,4} {2,}Lazy (non-greedy) versions
*? +?Groups & alternation
Capture group
(abc)Non-capturing group
(?:abc)Alternation (a or b)
a|bNamed group (Python/PCRE)
(?P<name>...)Backreference to group 1
\1Lookahead: positive / negative
(?=...) (?!...)Ready-to-use patterns
IPv4 address
\b([0-9]{1,3}\.){3}[0-9]{1,3}\bEmail address
[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}URL
https?://[^\s"'>]+MD5 hash (32 hex)
\b[a-fA-F0-9]{32}\bSHA-256 hash (64 hex)
\b[a-fA-F0-9]{64}\bMAC address
([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}AWS access key id
AKIA[0-9A-Z]{16}JWT
eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+Using regex on the CLI
grep with extended regex, only matching part
grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' file.txtPerl-compatible regex (lookarounds) in grep
grep -oP '(?<=password=)\S+' configsed capture-group replacement
sed -E 's/(user)=(\w+)/\1=REDACTED/' fileripgrep (fast recursive secret hunt)
rg -e 'AKIA[0-9A-Z]{16}' -e 'eyJ[A-Za-z0-9_-]+\.' .Tips
- grep needs -E for + ? { } | (extended) or -P for lookarounds (Perl) — basic grep treats them literally.
- Quantifiers are greedy by default; add ? to make them lazy when matching too much.
- Escape regex metacharacters . * + ? ( ) [ ] { } ^ $ | \ with a backslash to match them literally.
- Test patterns interactively at regex101.com before baking them into scripts.