Cheatsheet Pages / Regex Cheatsheet

Backreferences in replacements — $1 vs \1, and grep -P in the terminal

In the pattern you always write \1; in the replacement string the spelling depends on the engine — and in the terminal, grep -P gives you PCRE.

Patterns use the syntax shared by PCRE / Python / JavaScript; engine differences are flagged in the cheatsheet where they matter. Test any pattern at regex101.com — and pick the matching engine.

pattern:     (\w+)@(\w+)
replacement: $1 at $2
# "user@host" → "user at host" (JS/Python style)
echo "user@host" | grep -P -o '\w+@\w+'   # grep -P = PCRE in the terminal

Good to know

JavaScript/Python replacements use $1 (Python also \g<1>); sed/Vim use \1. Case-conversion escapes work in PCRE/Perl/Vim but not in Python or JavaScript.

Regex Cheatsheet one-page card — patterns that actually work
The full one-page card — Regex Cheatsheet. Get the complete cheatsheet on GitHub