This regex cheat sheet lists every regular expression token you are likely to need, from \d and \b to lookbehind, possessive quantifiers and \p{Script=Greek}, each with a short explanation and an example that shows exactly what it matches. Every example has a Try it link that opens our regex tester with the pattern, flags and sample text already filled in, so you can change it and watch the matches move.
Regex looks the same everywhere but is not. Lookbehind is unlimited in JavaScript and .NET, fixed-width in Python, bounded in Java and PCRE, and missing in Go. Python spells named groups (?P<name>…), PHP cannot use names in a replacement, and only some engines know \A, \K or (?R). The flavour picker shows each token's support in JavaScript, PCRE/PHP, Python, Go, Java and .NET, with the alternative syntax where one exists. The examples themselves run in JavaScript; for the other languages, read the notes and test in that language before you ship.
How to use
- Search for a token (\b, lookahead) or a task (email, trim, duplicate words), or jump to a section from the chips at the top.
- Pick your language in the flavour bar. Tokens it does not support are dimmed, and the note under each entry gives the syntax your language uses instead.
- Click Try it in the regex tester to run the example live, then edit the pattern or the text there.
- Use the # next to an entry to link straight to it, or Print / Save as PDF for a compact two-column copy.
Frequently asked questions
What is the difference between greedy and lazy quantifiers?
A greedy quantifier (*, +, {n,m}) takes as many characters as it can and then gives some back if the rest of the pattern fails; a lazy one (*?, +?, {n,m}?) takes as few as possible. On <b>bold</b>, <.+> matches the whole string while <.+?> matches <b> and </b> separately. Possessive quantifiers (*+, ++) are greedy and never give characters back; JavaScript, Go and .NET do not have them.
Which languages support lookbehind?
JavaScript (since ES2018) and .NET allow lookbehind of any length. Java requires a bounded maximum length, so no * or + inside. Python's re module accepts only fixed-width lookbehind. PCRE2 required each alternative to have a fixed length until 10.43, which added variable-length lookbehind up to a limit (255 characters by default). Go's RE2 engine has no lookahead or lookbehind at all.
How do I match any letter, including accented and non-Latin letters?
Use the Unicode property \p{L}. In JavaScript it needs the u or v flag (/\p{L}+/u); PCRE, Go, Java and .NET support it directly. [a-zA-Z] misses é, ß and every non-Latin script, and \w is ASCII-only in JavaScript and Go. Python's built-in re has no \p{…}; use the third-party regex module, or [^\W\d_], which in Python 3 matches Unicode letters.
How do I use a captured group in the replacement?
It depends on the language: $1 in JavaScript, Java, .NET, Go and PHP; \1 or \g<1> in Python. Named groups are $<name> in JavaScript, ${name} in Java, .NET and Go, and \g<name> in Python; PHP's preg_replace has no named form, so use the number. The whole match is $& in JavaScript and .NET, $0 in Java and PHP, and \g<0> in Python.
Why does my regex work in JavaScript but not in Python (or the other way round)?
The usual culprits: named groups ((?<name>) in JavaScript, (?P<name>) in Python), \p{…} properties (JavaScript yes, Python re no), lookbehind length (any in JavaScript, fixed in Python), inline flags ((?i) at the start works in Python but is an error in JavaScript, which only has the scoped (?i:…)), and \d or \w matching Unicode digits and letters in Python but only ASCII in JavaScript. Also remember Python string escaping: write patterns as raw strings, r"\d+".
Is it safe to validate email addresses with a regex?
Only as a shape check. A practical pattern such as [\w.+-]+@[\w-]+(?:\.[\w-]+)+ catches typos, but it rejects some valid addresses (quoted local parts, internationalised domains) and accepts some invalid ones. No pattern can tell you that a mailbox exists, so check the shape loosely and then send a confirmation email.
Writing patterns that work in more than one language
If a pattern must run in several engines (a shared validation rule, a config file read by services in different languages), stay inside the common subset. These behave the same in all six engines on this page:
• Literal characters, escapes such as \. and \$, and the classes [abc], [^abc] and [a-z].
• \d, \s and \w, as long as your input is ASCII (Python and .NET match Unicode digits and letters too).
• ^ and $, \b, alternation, capturing and non-capturing groups.
• Greedy and lazy quantifiers: *, +, ?, {n}, {n,m} and their ? forms.
Leave out lookaround (Go has none), backreferences (Go), possessive quantifiers and atomic groups (JavaScript, Go and .NET lack one or both), named groups (Python's syntax differs) and \p{…} (Python's re lacks it). When you need one of those, keep one copy of the pattern per language and test each.
Avoiding catastrophic backtracking
Backtracking engines (JavaScript, PCRE, Python, Java, .NET) can take exponential time on patterns where a quantified group can match the same text in many ways, such as (a+)+$ or (\w|\d)*x. On a long input that almost matches, the engine tries every split before failing, and a web server can hang on one request. This is the regular expression denial of service (ReDoS) problem.
The fixes are mechanical: avoid nesting quantifiers over the same characters; make alternatives mutually exclusive; prefer a negated class such as [^"]* to .*? when you know the delimiter; use possessive quantifiers or atomic groups where your engine has them; and cap input length before matching. .NET also offers RegexOptions.NonBacktracking and a match timeout, PCRE has match limits you can set, and Go's RE2 engine guarantees linear time by not supporting the features that need backtracking.
Escaping regex inside strings
Most regex bugs in real code are string-escaping bugs, not regex bugs. The backslash is special to both the language's string literal and the regex engine, so a pattern written as a normal string needs double backslashes.
• JavaScript: a literal /\d+/ needs one backslash; new RegExp("\\d+") needs two. String.raw`\d+` avoids the doubling.
• Python: always use raw strings, r"\d+"; otherwise "\b" is a backspace character, not a word boundary.
• Java and C#: "\\d+" in ordinary strings (Java text blocks process escapes too); C# verbatim strings, @"\d+", keep backslashes as typed.
• Go: backtick raw strings, `\d+`, are the norm with regexp.MustCompile.
• PHP: patterns include delimiters, '/\d+/', and single-quoted strings keep most backslashes as typed.
