TExcelTAD
Home › Regular Expressions › Regex Cheat Sheet

Regex Cheat Sheet

A compact reference for everything in this course. Syntax is JavaScript unless noted; most of it works the same in Python, Java, .NET and PCRE.

Characters

Pattern Matches
abc the literal text abc
. any character except a line break (any at all with s)
\. \* \\ a literal ., *, \ (escape metacharacters)
\n \t \r newline, tab, carriage return
￿ / \u{1F600} character by code (braces need u/v)

Classes

Pattern Matches
[abc] one of a, b, c
[^abc] one character except a, b, c
[a-z0-9] one character in the ranges
\d / \D digit / non-digit
\w / \W [A-Za-z0-9_] / anything else
\s / \S whitespace / non-whitespace
\p{L} \p{N} \p{Lu} Unicode letter, number, uppercase letter (u/v)
[\p{L}--[a-z]] set subtraction (v only)

Quantifiers

Greedy Lazy Count
* *? 0 or more
+ +? 1 or more
? ?? 0 or 1
{n} — exactly n
{n,} {n,}? n or more
{n,m} {n,m}? n to m

Anchors and boundaries

Pattern Position
^ / $ start / end of input (of each line with m)
\b / \B word boundary / not a boundary
\A \z very start / end — Python, Java, .NET, PCRE only

Groups and references

Pattern Meaning
(abc) capturing group (numbered)
(?:abc) non-capturing group
(?<name>abc) named group (Python: (?P<name>abc))
\1 / \k<name> backreference in the pattern
a|b alternation: a or b

Lookarounds

Pattern Succeeds if…
X(?=Y) X is followed by Y
X(?!Y) X is not followed by Y
(?<=Y)X X is preceded by Y
(?<!Y)X X is not preceded by Y

Flags (JavaScript)

Flag Effect
g all matches
i ignore case
m ^/$ per line
s . matches newlines
u / v Unicode mode / Unicode sets
y sticky (match at lastIndex)
d group indices

Replacement strings

JS Python Inserts
amp;
\g<0> whole match
$1 \1 group 1
lt;name>
\g<name> named group
$ $ a literal dollar (Python needs no escape)

JavaScript methods

Call Result
re.test(s) boolean
s.match(re) first match details, or all strings with g
[...s.matchAll(re_g)] all matches with groups
s.replace(re, str | fn) new string
s.split(re) array
re.exec(s) next match (stateful with g/y)

Python re

Call Result
re.search(p, s) first match or None
re.fullmatch(p, s) whole string must match
re.findall(p, s) strings, or tuples of groups
re.finditer(p, s) iterator of match objects
re.sub(p, r, s) replace all
re.split(p, s) split

Everything in one example

regex · try it yourself
/(?<=^|\s)(?<tag>#[\p{L}\d_]+)(?=\s|$)|(?<num>-?\b\d+(?:\.\d+)?\b)/gu
#café opens at 7 with -2.5 deals #bánh_mì
4 matches
#1 "#café" at index 0
   group tag: "#café"
   group num: undefined
#2 "7" at index 15
   group tag: undefined
   group num: "7"
#3 "-2.5" at index 22
   group tag: undefined
   group num: "-2.5"
#4 "#bánh_mì" at index 33
   group tag: "#bánh_mì"
   group num: undefined
js · try it yourself
const s = "Sale: 3 items at $4.50, total $13.50 (#deal #weekend)";
console.log(s.match(/\$\d+(?:\.\d\d)?/g));
console.log([...s.matchAll(/#(\w+)/g)].map((m) => m[1]));
console.log(s.replace(/\$(\d+)\.(\d\d)/g, "$1,$2 USD"));
console.log(/^Sale:/.test(s), s.search(/\(/));
[ '$4.50', '$13.50' ]
[ 'deal', 'weekend' ]
Sale: 3 items at 4,50 USD, total 13,50 USD (#deal #weekend)
true 37

Golden rules

  1. Anchor validation patterns: ^…$.
  2. Prefer specific classes ([^"]*) over .*.
  3. Use non-capturing groups by default, named groups for what you read.
  4. Turn on u for user-facing text.
  5. Avoid nested quantifiers over the same characters.
  6. Test with text that should and shouldn't match.
  7. If a regex grows unreadable, split the work with code.

Test yourself

Which construct matches a position followed by a digit, without consuming it?

  1. (?:\d)
  2. (?=\d)
  3. (?<=\d)
  4. [\d]

Which is the lazy version of {2,5}?

  1. {2,5}+
  2. {2,5}?
  3. ?{2,5}
  4. {?2,5}

What does

amp; mean in a JavaScript replacement string?

  1. The first group
  2. The whole match
  3. A literal ampersand
  4. The text after the match