RegEx Cheatsheet

Reference

Overview

RegEx patterns are made up of characters, groups and ranges:

  • Characters: match a single character
  • Group: match all chracters in group (AND)
  • Range: match any character in range (OR)

Characters

RegExMeaning
.Anything except \n, \r. Means literal dot in []
\dDigit
\DNot digit
\sWhitespace (tab, space, newline, etc.)
\SNot whitespace
\w[A-Za-z0-9_]
\W[^A-Za-z0-9_]
\t, \r, \nNormal meanings
$end of string
^start of string

Quantifiers

RegExMeaning
x?0 or 1 time
x{a,b}x between a and b times (inclusive)
x+1 or more
x*0 or more
x<quantifier>?Non-greedy match (as little string as possible)
$end of string

Lookahead / Lookbehind

RegExMeaning
x(?=y)match x if x is followed by y
x(?!y)match x if x is not followed by y
x(?<=y)match x if x is preceded by y
x(?<!y)match x if x is not preceded by y

Groups and Ranges

RegExMeaning
x|yx or y (default global group, use inside group / range)
[<characters>]match any enclosed character
[^<characters>]match any non-enclosed character
(x)capturing group, use via $1, ..., $9
\nbackreference to group n
(?:x)non-capturing group

See here on how to display | inside a Markdown table.