Skip to content
OnlineToolStudio

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

abcThe 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 \tNew line, carriage return, tab
\x41 \u00e9A 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 \DA digit 0–9 / not a digit
\w \WA word character (letter, digit, _) / not one
\s \SWhitespace (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$
\bA word boundary\bcat\b → cat, not catalog
\BNot 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|ba 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

gGlobal — find every match, not just the first
iIgnore case
mMultiline — ^ and $ match at line starts and ends
sDot-all — . also matches new lines
uUnicode — enables \p{…} and \u{…}
ySticky — match only at the current position

Replacement

$1 $2The 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

JavaScriptPHPPythonJava
Pattern/\d+/g'/\d+/'r'\d+'"\\d+"
Find alltext.matchAll(re)preg_match_all()re.findall()while (m.find())
Replacetext.replace(re, s)preg_replace()re.sub()replaceAll()
Ignore casei flagi modifierre.IGNORECASECASE_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 textmanual / RegExp.escapepreg_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

  1. Scroll the sections, or search for a symbol or word (lookbehind, \b, lazy…).
  2. Read the example next to each symbol.
  3. 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.

Related tools

All tools