SpunkArt
Data, text and encoding

Regular expression cheat sheet

A quick reference to the regex syntax shared by JavaScript, Python, PCRE and most editors, with a tiny example beside each token. The last table shows where the common flavours disagree, which is where most copy-pasted patterns break.

Single characters

TokenMatchesExample
.Any character except a line breakh.t finds hat, hot, h9t
\d / \DA digit / not a digit\d\d finds 42 in “room 42”
\w / \WLetter, digit or underscore / anything else\w+ finds each word in “snake_case id”
\s / \SWhitespace / not whitespace\s+ finds runs of spaces and tabs
\t \n \rTab, line feed, carriage return\r\n finds Windows line endings
\. \* \?The literal symbol3\.14 finds “3.14” but not “3714”

The characters that need a backslash to be literal are . ^ $ * + ? ( ) [ ] { } | \ and, inside a JavaScript literal, /.

Character classes

TokenMatchesExample
[abc]One of a, b or cgr[ae]y finds grey and gray
[^abc]Any one character except those[^,]+ finds each field between commas
[a-z]A range[A-Fa-f0-9] finds one hex digit
[-.]Literal dash or dotA dash first or last in the class is literal

Anchors and boundaries

TokenPositionExample
^Start of text (or of each line with the m flag)^# finds lines that start with #
$End of text (or of each line with m);$ finds a trailing semicolon
\bBetween a word character and a non-word character\bcat\b finds “cat” but not “concat”
\BNot a word boundary\Bcat finds the cat inside “concat”

Quantifiers

TokenRepeats the item before itExample
*0 or moreab*c finds ac, abc, abbbc
+1 or more\d+ finds 7 and 2048
?0 or 1 (optional)colou?r finds color and colour
{3}Exactly 3\d{3} finds 3-digit runs
{2,4}2 to 4x{2,4} finds xx, xxx, xxxx
{2,}2 or more\n{2,} finds blank-line gaps
*? +? ??Lazy versions: as few as possible".+?" stops at the first closing quote

Quantifiers are greedy by default: they take as much as they can, then give back only what the rest of the pattern needs.

Groups, alternation and references

TokenMeaningExample
a|ba or bjpe?g|png finds jpg, jpeg, png
(…)Capturing group, numbered from 1(\w+)@ captures the name before @
(?:…)Group without capturing(?:ab)+ finds abab
(?<name>…)Named group (JavaScript, PCRE)(?<user>\w+)
\1Back-reference: the same text as group 1\b(\w+) \1\b finds “the the”
$1 or \1Group 1 in a replacement stringSwap “Last, First”: (\w+), (\w+) → $2 $1

Lookarounds

Lookarounds check what is next to the current position without including it in the match.

TokenConditionExample
(?=…)Followed by\d+(?=px) finds 12 in “12px”
(?!…)Not followed by\d+(?!px|\d) finds 3 in “3em”
(?<=…)Preceded by(?<=\$)\d+ finds 40 in “$40”
(?<!…)Not preceded by(?<!-)\b\d+ skips negative numbers

Flags

  • i – ignore case.
  • g – find all matches, not just the first (JavaScript; other languages use a function such as findall).
  • m – multi-line: ^ and $ work per line.
  • s – “dot-all”: . also matches line breaks.
  • u – Unicode mode in JavaScript, needed for \p{L} (any letter) and for emoji to count as one character.
  • x – verbose mode in Python and PCRE: whitespace and # comments inside the pattern are ignored.

Where flavours differ

FeatureJavaScriptPython rePCRE (PHP, grep -P)POSIX ERE (grep -E)
Named group(?<n>…)(?P<n>…)Both formsNone
Lazy *?YesYesYesNo
LookbehindYesFixed width onlyYes, with limits on lengthNo
\dYesYes (Unicode digits by default)YesUse [0-9] or [[:digit:]]
\p{L}With u flagNot in reYesNo

Questions

Why does my pattern work in one tool but not another?

Check the flavour table first, then escaping. In a JavaScript or Python string literal, a backslash must often be doubled, so \d becomes "\\d" unless you use a regex literal or a raw string such as r"\d".

Should I use regex to parse HTML or JSON?

No. Use a real parser. Regex is fine for finding simple tokens inside text, but nested structures defeat it.

How do I try these patterns?

Paste any example into the regex tester, which also explains how to build a pattern against test cases.