Regex Cheat Sheet

Quick reference for regular expression patterns - now with Common patterns and Flags/Modifiers

CommonMost Used
[abc]A single character of: a, b or c

Example: [abc] matches a, b, or c

[^abc]A character except: a, b or c

Example: [^abc] matches d, e, f...

[a-z]A character in the range: a-z

Example: [a-z] matches lowercase letters

[^a-z]A character not in the range: a-z

Example: [^a-z] matches uppercase, digits, symbols

[a-zA-Z]A character in the range: a-z or A-Z

Example: [a-zA-Z] matches any letter

.Any single character

Example: a.c matches abc, a1c, a@c

a|bAlternate - match either a or b

Example: cat|dog matches cat or dog

\sAny whitespace character

Example: \s+ matches spaces, tabs, newlines

\SAny non-whitespace character

Example: \S+ matches visible characters

\dAny digit

Example: \d+ matches 123

\DAny non-digit

Example: \D+ matches abc

\wAny word character

Example: \w+ matches hello123

\WAny non-word character

Example: \W+ matches @#$

(?:...)Non-capturing group

Example: (?:abc)+ matches abc sequences

(...)Capturing group

Example: (\d+) captures numbers

a?Zero or one of a

Example: ab?c matches ac, abc

a*Zero or more of a

Example: ab*c matches ac, abc, abbc

a+One or more of a

Example: ab+c matches abc, abbc

a{3}Exactly 3 of a

Example: \d{3} matches 123

a{3,}3 or more of a

Example: \d{3,} matches 123, 1234...

a{3,6}Between 3 and 6 of a

Example: \d{3,6} matches 123, 1234, 12345, 123456

^Start of string

Example: ^hello matches hello at start

$End of string

Example: world$ matches world at end

\bA word boundary

Example: \bcat\b matches cat as whole word

\BNon-word boundary

Example: \Bcat\B matches cat within word

Character classes
\w \d \sword, digit, whitespace

Example: \w+ \d+ \s+ matches word123 space

\W \D \Snot word, digit, whitespace

Example: \W+ matches @#$

[a-g]character between a & g

Example: [a-g] matches a, b, c, d, e, f, g

[[:alnum:]]Letters and digits

Example: [[:alnum:]] matches alphanumeric characters

[[:alpha:]]Letters

Example: [[:alpha:]] matches alphabetic characters

[[:ascii:]]ASCII codes 0-127

Example: [[:ascii:]] matches ASCII characters

[[:blank:]]Space or tab only

Example: [[:blank:]] matches space and tab

[[:cntrl:]]Control characters

Example: [[:cntrl:]] matches control characters

[[:digit:]]Decimal digits

Example: [[:digit:]] matches 0-9

[[:graph:]]Visible characters (not space)

Example: [[:graph:]] matches printable non-space

[[:lower:]]Lowercase letters

Example: [[:lower:]] matches a-z

[[:print:]]Visible characters

Example: [[:print:]] matches printable characters

[[:punct:]]Visible punctuation characters

Example: [[:punct:]] matches punctuation

[[:space:]]Whitespace

Example: [[:space:]] matches whitespace characters

[[:upper:]]Uppercase letters

Example: [[:upper:]] matches A-Z

[[:word:]]Word characters

Example: [[:word:]] matches word characters

[[:xdigit:]]Hexadecimal digits

Example: [[:xdigit:]] matches 0-9A-Fa-f

[[:<:]]Start of word

Example: [[:<:]] matches word start boundary

[[:>:]]End of word

Example: [[:>:]] matches word end boundary

Anchors
\GStart of match

Example: \G\d+ matches digits from last match position

^Start of string

Example: ^hello matches hello at start of string

$End of string

Example: world$ matches world at end of string

\AStart of string

Example: \Ahello matches hello at very start

\ZEnd of string

Example: world\Z matches world at very end

\zAbsolute end of string

Example: end\z matches end at absolute end

\bA word boundary

Example: \bword\b matches whole word

\BNon-word boundary

Example: \Bcat\B matches cat within word

Escaped characters
\. \* \\escaped special characters

Example: \. matches literal dot

\+reserved characters

Example: \+ matches literal plus sign

\000octal escape

Example: \000 matches null character

\xFFhexadecimal escape

Example: \xFF matches character with hex value FF

\uFFFFunicode escape

Example: \u0041 matches character A

\u{FFFF}extended unicode escape

Example: \u{1F600} matches emoji

\cIcontrol character escape

Example: \cI matches tab character

\ttab

Example: \t matches tab character

\nline feed

Example: \n matches newline character

\vvertical tab

Example: \v matches vertical tab

\fform feed

Example: \f matches form feed character

\rcarriage return

Example: \r matches carriage return

\0null

Example: \0 matches null character

Groups & Lookaround
(?:...)Non-capturing group

Example: (?:abc)+ matches abc sequences

(...)Capturing group

Example: (\d+) captures numbers

(?>...)Atomic group (non-capturing)

Example: (?>\d+) matches without backtracking

(?|...)Duplicate/reset subpattern group number

Example: (?|(a)|(b)) resets group numbering

(?#...)Comment group

Example: (?#this is a comment)abc

(?'name'...)Named Capturing Group

Example: (?'word'\w+) captures named group

(?<name>...)Named Capturing Group

Example: (?<word>\w+) captures named group

(?P<name>...)Named Capturing Group

Example: (?P<word>\w+) Python-style named group

(?imsxUJnxx)Inline modifiers

Example: (?i)abc matches case-insensitive

(?imsxUJnxx:...)Localized inline modifiers

Example: (?i:abc) applies case-insensitive to abc only

(?(1)yes|no)Conditional statement

Example: (a)?(?(1)b|c) matches ab or c

(?(R)yes|no)Conditional statement

Example: (?(R)recursive|normal) checks recursion

(?(R#)yes|no)Recursive Conditional statement

Example: (?(R#)recursive|normal) checks specific recursion

(?(R&name)yes|no)Conditional statement

Example: (?(R&name)yes|no) checks named recursion

(?(?=...)yes|no)Lookahead conditional

Example: (?(?=\d)\d+|\w+) matches digits or words

(?(?<=...)yes|no)Lookbehind conditional

Example: (?(?<=@)\w+|\d+) matches based on lookbehind

(?(DEFINE)...)Pre-define patterns before using them

Example: (?(DEFINE)(?<word>\w+))(?&word)

(?R)Recurse entire pattern

Example: \(((?>[^()]++|(?R))*)\) matches nested parentheses

(?1)Match expression defined in capture group 1

Example: (\w+)\s+(?1) matches repeated words

(?+1)Match expression defined in the first relative capture group

Example: (?+1) references relative group

(?&name)Match expression defined in capture group `name`

Example: (?<word>\w+)\s+(?&word) matches repeated named groups

(?P=name)Match subpattern `name`

Example: (?P<word>\w+)\s+(?P=word) Python-style backreference

(?P>name)Match expression defined in the capture group "{name}"

Example: (?P<word>\w+)\s+(?P>word) Python-style subroutine

\1backreference to group #1

Example: (\w)\1 matches doubled letters

(?=...)Positive Lookahead

Example: \d+(?=px) matches numbers before px

(?!...)Negative Lookahead

Example: \d+(?!px) matches numbers not before px

(?<=...)Positive Lookbehind

Example: (?<=\$)\d+ matches numbers after $

(?<!...)Negative Lookbehind

Example: (?<!un)happy matches happy not preceded by un

(*ACCEPT)Control verb

Example: (*ACCEPT) forces match to succeed

(*FAIL)Control verb

Example: (*FAIL) forces match to fail

(*MARK:NAME)Control verb

Example: (*MARK:label) marks position for backtracking

Quantifiers & Alternation
a?Zero or one of a

Example: ab?c matches ac, abc

a*Zero or more of a (greedy)

Example: ab*c matches ac, abc, abbc

a+One or more of a

Example: ab+c matches abc, abbc

a{3}Exactly 3 of a

Example: \d{3} matches 123

a{3,}3 or more of a

Example: \d{3,} matches 123, 1234...

a{3,6}Between 3 and 6 of a

Example: \d{3,6} matches 123, 1234, 12345, 123456

a*?Lazy quantifier

Example: a*? matches as few a's as possible

a*+Possessive quantifier

Example: a*+ matches without backtracking

ab|cdmatch ab or cd

Example: cat|dog matches cat or dog

Flags/ModifiersNew
gGlobal

Example: /abc/g matches all occurrences

mMultiline

Example: /^abc/m matches abc at line start

iCase insensitive

Example: /abc/i matches ABC, abc, Abc

xIgnore whitespace / verbose

Example: Allows comments and whitespace

sSingle line

Example: . matches newline characters

uUnicode

Example: Enables Unicode matching

XeXtra

Example: Extra features (Perl)

UUngreedy

Example: Makes quantifiers non-greedy by default

ySticky

Example: Matches from lastIndex position

dhasIndices

Example: Generate indices for substring matches

Substitution
$0Complete match contents

Example: Replace with entire matched text

$1Contents in capture group 1

Example: Replace with first captured group

$$Insert a dollar sign

Example: Replace with literal $ character

${foo}Contents in capture group `foo`

Example: Replace with named group 'foo'

\x20Hexadecimal replacement values

Example: Replace with space character (hex 20)

\x{06fa}Hexadecimal replacement values

Example: Replace with Unicode character U+06FA

\tInsert a tab

Example: Replace with tab character

\rInsert a carriage return

Example: Replace with carriage return character

\nInsert a newline

Example: Replace with newline character

\fInsert a form-feed

Example: Replace with form-feed character

\UUppercase Transformation

Example: Convert following text to uppercase

\LLowercase Transformation

Example: Convert following text to lowercase

\ETerminate any Transformation

Example: End case transformation

${1:+foo:bar}Conditional replacement

Example: Replace with 'foo' if group 1 exists, else 'bar'

\[Insert the escaped literal

Example: Replace with literal [ character