Regex Cheat Sheet
runs in your browserno sign-upfree
Every regex symbol on one page — classes, anchors, quantifiers, groups, lookarounds, flags and replacements — with examples and a JS/PHP/Python/Java comparison.
Characters
| abc | The text "abc"abc in "abcdef" |
| . | Any character except a new line (any at all with the s flag)c.t → cat, cut, c7t |
| \. \* \? \\ | A special character used literally3\.14 → 3.14 |
| \n \r \t | New line, carriage return, tab |
| \x41 \u00e9 | A character by its hex code (A, é) |
| \u{1F600} | A character beyond U+FFFF (needs the u flag) |
Character classes
| [abc] | One of a, b or cgr[ae]y → gray, grey |
| [^abc] | Any character except a, b or c |
| [a-z] [0-9] | A character in a range[A-Za-z] |
| \d \D | A digit 0–9 / not a digit |
| \w \W | A word character (letter, digit, _) / not one |
| \s \S | Whitespace (space, tab, new line…) / not whitespace |
| \p{L} \p{N} | Any letter / any number in any language (u flag)\p{L}+ → café, नमस्ते |
| \p{Emoji} | Emoji characters (u flag) |
Anchors and boundaries
| ^ | Start of the text (start of each line with m)^Hello |
| $ | End of the text (end of each line with m)end$ |
| \b | A word boundary\bcat\b → cat, not catalog |
| \B | Not a word boundary |
Quantifiers
| * | 0 or more timesab*c → ac, abc, abbbc |
| + | 1 or more timesab+c → abc, abbc |
| ? | 0 or 1 time (optional)colou?r → color, colour |
| {3} | Exactly 3 times\d{3} |
| {2,5} {2,} | 2 to 5 times / 2 or more times |
| *? +? ?? | Lazy: as few times as possible<.+?> → each tag, not all of them |
Groups and references
| (abc) | Capture group — its text is kept as $1, $2…(\d{4})-(\d\d) |
| (?<name>abc) | Named capture group(?<year>\d{4}) |
| (?:abc) | Group without capturing(?:ab)+ |
| a|b | a or bcat|dog |
| \1 \k<name> | The same text as group 1 / the named group matched(\w)\1 → ll, ee |
Lookarounds
| (?=abc) | Followed by abc (not part of the match)\d+(?=€) → 20 in "20€" |
| (?!abc) | Not followed by abc\d+(?!€) |
| (?<=abc) | Preceded by abc(?<=\$)\d+ → 20 in "$20" |
| (?<!abc) | Not preceded by abc |
Flags
| g | Global — find every match, not just the first |
| i | Ignore case |
| m | Multiline — ^ and $ match at line starts and ends |
| s | Dot-all — . also matches new lines |
| u | Unicode — enables \p{…} and \u{…} |
| y | Sticky — match only at the current position |
Replacement
| $1 $2 | The text of group 1, 2…(\w+) (\w+) → "$2 $1" swaps two words |
| $<name> | The text of a named group |
| $& | The whole match |
| $` $' | The text before / after the match |
| $$ | A literal $ sign |
JavaScript, PHP, Python and Java compared
| JavaScript | PHP | Python | Java | |
|---|---|---|---|---|
| Pattern | /\d+/g | '/\d+/' | r'\d+' | "\\d+" |
| Find all | text.matchAll(re) | preg_match_all() | re.findall() | while (m.find()) |
| Replace | text.replace(re, s) | preg_replace() | re.sub() | replaceAll() |
| Ignore case | i flag | i modifier | re.IGNORECASE | CASE_INSENSITIVE |
| Named group | (?<name>…) | (?<name>…) | (?P<name>…) | (?<name>…) |
| Group in replacement | $1 $<name> | $1 ${1} | \1 \g<name> | $1 ${name} |
| Whole match in replacement | $& | $0 | \g<0> | $0 |
| Unicode letters | \p{L} (u flag) | \p{L} (u modifier) | regex package | \p{L} |
| Escape text | manual / RegExp.escape | preg_quote() | re.escape() | Pattern.quote() |
About this tool
A complete, searchable regular expression cheat sheet: characters and escapes, character classes, anchors and word boundaries, greedy and lazy quantifiers, capture and named groups, back-references, lookahead and lookbehind, flags and replacement syntax — each with a short example. A comparison table shows how the same things are written in JavaScript, PHP, Python and Java.
How to use
- Scroll the sections, or search for a symbol or word (lookbehind, \b, lazy…).
- Read the example next to each symbol.
- Try it out in the Regex Tester, which also explains your pattern piece by piece.
FAQ
What is the difference between greedy and lazy?
Greedy quantifiers (*, +, {2,}) match as much as they can; lazy ones (*?, +?) as little as possible. On "<b>x</b>", <.+> matches the whole string, while <.+?> matches each tag separately.
What does (?:…) mean?
It groups part of a pattern — for example to repeat it with (?:ab)+ or to limit an alternative — without capturing it as $1. It keeps group numbers simple and is slightly faster.
Is the syntax the same in every language?
The basics are. The differences are mostly in how you write the pattern (/…/ in JavaScript, '/…/' in PHP, r'…' in Python, doubled backslashes in Java), named groups in Python ((?P<name>…)) and replacement syntax ($1 vs \1). See the comparison table at the end.