Regular expressions cheatsheet

A quick reference for some common regular expressions.

13 July 2023



Find this OR that – either or both return true

# | = OR


Matches this OR that

Character sets

Character sets, denoted by a pair of brackets [], let us match one character from a series of characters, allowing for matches with incorrect or different spellings.


Matches consensus, concensus, consencus, concencus.


Matches c, a, or t, but not the text cat.

# ^ = NOT

Matches anything NOT c, a, or t, e.g. dog


Wildcards will match any single character (letter, number, symbol or whitespace) in a piece of text. They are useful when we do not care about the specific value of a character, but only that a character exists.

# . = Wildcard
# \. = Escape wildcard

Matches any 9 character text

I have . cats

Matches I have 2 cats, I have 8 cats, I have X cats etc.


Ranges allow us to specify a range of characters in which we can make a match without having to type out each individual character.


Matches any one number between 0-9


Matches any one lowercase letter between a-z


Matches any one uppercase or lowercase letter between A-z

Shorthand Character Classes

\w # Word character

Represents the regex range [A-Za-z0-9_], and it matches a single uppercase character, lowercase character, digit or underscore

\d # Digit character

Represents the regex range [0-9], and it matches a single digit character

\s # Whitespace character

Represents the regex range [ \t\r\n\f\v], matching a single space, tab, carriage return, line break, form feed, or vertical tab.


Matches a digit character, followed by a whitespace character, followed by 7 word characters, eg. 3 monkeys

\W # Non-word character
\D # Non-digit character
\S # Non-whitespace character


Put it (here|there)

Matches Put it here OR Put it there



Matches any three character words


Match any words with a minimum of 4 characters and max 7 characters.

# Matches ro aaa r

Matches min 3 a's, max 7 a's. Will always match with the largest quantity.

? # Optional quantifier 

Matches both color and colour as the u is marked as optional
Need to escape ? if want to use it in sentance.

The monkey ate a (rotten )?banana

Matches both The monkey ate a rotten banana and The monkey ate a banana.

Kleene star - matches 0 or more times


Matches co followed by 0 or more o's, followed by l. So, col, cool, coooooooooooooool all match.

+ # Kleene plus - matches 1 or more times.

Matches co followed by 1 or more o's, followed by l
So, cool, coool, coooooooooooooool all match.


Anchor metacharacters ensure we don't match unintended text. ^ and $ are used to mark the start and the end of a string.

^Red is my favourite colour$

Matches Red is my favourite colour, but not Orange Red is my favourite colour of car.