Developer Toolbox

Regex Cheatsheet

The syntax of regular expressions on one page, each item with an example you can open in the Regex Tester, and ready-made patterns written out for six languages.

Open Regex Tester Type a pattern and some text to see what it matches.

Characters and classes

.Any character except a line break (with s, line breaks too).c.t (open in Regex Tester)
\dA digit. In JavaScript only 0-9; in Python 3 any Unicode digit.\d+ (open in Regex Tester)
\wA word character: letters, digits, underscore (ASCII in JavaScript).\w+ (open in Regex Tester)
\sWhitespace: space, tab, line breaks and more.\s+ (open in Regex Tester)
\D \W \SThe opposite of \d, \w, \s.\D+ (open in Regex Tester)
[abc]One of the listed characters.gr[ae]y (open in Regex Tester)
[^abc]Any character except the listed ones.[^aeiou\s]+ (open in Regex Tester)
[a-z]A range. Combine them: [A-Za-z0-9_].[A-F0-9]+ (open in Regex Tester)
\bA word boundary: between \w and something else.\bcat\b (open in Regex Tester)

Anchors

^Start of the string (with m, of each line).^\w+ (open in Regex Tester)
$End of the string (with m, of each line).\w+$ (open in Regex Tester)
\A \zStart and end of the whole string, whatever the flags, in Java, PHP, Go and .NET. Python writes the end as \Z (\z too from 3.14). JavaScript has neither; use ^ and $ without m.

Quantifiers

*Zero or more.ab*c (open in Regex Tester)
+One or more.ab+c (open in Regex Tester)
?Zero or one: makes the item optional.colou?r (open in Regex Tester)
{3}Exactly 3.\d{3} (open in Regex Tester)
{2,}2 or more.\d{2,} (open in Regex Tester)
{2,4}From 2 to 4.\d{2,4} (open in Regex Tester)
*? +? ??Lazy: as few as possible. <.+?> stops at the first >.<.+?> (open in Regex Tester)

Groups and lookaround

(abc)A capturing group: matched text is kept as $1, \1, group(1).(\d{4})-(\d{2}) (open in Regex Tester)
(?:abc)A group that does not capture: for alternation and quantifiers.(?:ab)+ (open in Regex Tester)
(?<year>\d{4})A named group. Python's re writes it (?P<year>...).(?<year>\d{4}) (open in Regex Tester)
a|bEither side.cat|dog (open in Regex Tester)
\1A backreference: the text group 1 matched. Not in Go.(\w)\1 (open in Regex Tester)
(?=...)Followed by, without consuming it. Not in Go.\d+(?=px) (open in Regex Tester)
(?!...)Not followed by. Not in Go.\d+(?!px|\d) (open in Regex Tester)
(?<=...)Preceded by. Not in Go.(?<=\$)\d+ (open in Regex Tester)
(?<!...)Not preceded by. Not in Go.(?<![\$\d])\d+ (open in Regex Tester)

Flags in JavaScript

gGlobal: find every match, not just the first.
iIgnore case.
mMultiline: ^ and $ match at line breaks.
sDot-all: . matches line breaks too.
uUnicode: code points instead of UTF-16 units, and \p{...} classes.
vLike u, plus set operations in classes. Use one of u or v.
ySticky: match only at lastIndex.
dIndices: report where each group starts and ends.

Characters to escape

Outside a class, put a backslash before any of . * + ? ^ $ ( ) [ ] { } | \ to match it literally, and before / inside a JavaScript literal. Inside [...], only ] \ ^ - need it, and only where they would otherwise mean something. With the v flag, a class also needs a backslash before ( ) [ ] { } / - |, and doubled punctuation such as && or !! is reserved.

Flavors differ

  • Go (RE2) has no lookaround and no backreferences. In exchange every match runs in linear time.
  • Python's re names groups (?P<name>...); JavaScript, Java and .NET write (?<name>...).
  • \d is 0-9 in JavaScript, but any Unicode digit in Python 3 and .NET unless you ask for ASCII.
  • Python's re.match anchors only at the start; re.fullmatch and Java's matches() test the whole string.

Ready-made patterns

16 guides