Regex Cheat Sheet
Quick reference for regular expression syntax across flavours.
Overview
A quick reference for regex syntax across the flavours you'll see in the wild - PCRE/Perl/Python, JavaScript, .NET, Java, POSIX. Entries cover character classes, quantifiers, anchors, groups, lookaround, backreferences, and the most-cited flag modifiers, with a note on which engines support each feature.
It's for developers who write regex often enough to need a refresher but not often enough to memorise every gotcha. Reach for it when porting a pattern between languages, debugging an unexpected match, or pairing with someone who learned regex from a different tradition.
How it works
The cheat sheet is organised by feature rather than by engine - character classes, quantifiers, anchors, groups, lookaround. Each entry shows the canonical syntax and flags differences across engines (e.g. JavaScript's lack of \K, .NET's named groups using (?<name>), Python's re.VERBOSE flag).
Coverage targets the most-used engines documented in the respective language references: ECMAScript regex (TC39), .NET regex (Microsoft docs), Python's re module, Java's java.util.regex, and PCRE2.
Examples
- Character classes:
\d digit \w word char \s whitespace [a-z] range [^abc] negation - Quantifiers:
* 0 or more + 1 or more ? 0 or 1 {n,m} between n and m +? lazy match - Anchors and lookaround:
^ start of line \b word boundary (?=foo) lookahead (?<=foo) lookbehind - Named groups:
PCRE/Python: (?P<name>...) .NET/Java/JS: (?<name>...)
FAQ
Why doesn't lookbehind work in my regex?
JavaScript only added variable-width lookbehind in ES2018, and some engines (older Node, Safari) lacked it for years. .NET, PCRE, Python, and Java support it.
What's the difference between greedy and lazy?
Greedy quantifiers (*, +) match as much as possible; lazy (*?, +?) match as little. Use lazy when matching against repeated delimiters: <.*?> won't span across multiple tags.
Is \K supported everywhere?
No - \K (resets the match start) is PCRE/Perl/Ruby. JavaScript and .NET don't support it - use lookbehind instead.
Why does my regex slow to a crawl?
Catastrophic backtracking - usually caused by alternation inside repetition ((a|a)+). Refactor to a non-ambiguous pattern or use atomic groups / possessive quantifiers if your engine supports them.