Regular Expressions (Regex) Cheat Sheet

A Regular Expression (Regex) is a pattern used to search, validate, extract, or replace text.

Example:

\d+

Matches one or more digits.


Literal Characters

Most characters match themselves.

RegexMatches
catcat
hellohello
.Any character (except newline by default)
\.A literal .
\**
\\\

Character Classes

Match one character from a set.

RegexMeaning
[abc]a, b, or c
[a-z]Lowercase letters
[A-Z]Uppercase letters
[0-9]Digits
[A-Za-z]Letters
[A-Za-z0-9]Alphanumeric
[^abc]Anything except a, b, or c
[^0-9]Not a digit

Shorthand Character Classes

RegexMeaning
.Any character
\dDigit (0-9)
\DNot a digit
\wWord character (a-zA-Z0-9_)
\WNot a word character
\sWhitespace
\SNon-whitespace

Quantifiers

Control how many times something appears.

RegexMeaning
*0 or more
+1 or more
?0 or 1
{3}Exactly 3
{2,5}Between 2 and 5
{3,}3 or more
{,5}*Up to 5 (not supported everywhere)

Examples

a*
"", "a", "aa", "aaa"
\d{4}

Matches

2026
1984
0001

Greedy vs Lazy

By default, quantifiers are greedy.

<.*>

Matches

<div>Hello</div>

as one match.

Lazy quantifiers stop as soon as possible.

<.*?>

Produces

<div>
</div>

Anchors

Anchors match positions instead of characters.

RegexMeaning
^Beginning of line/string
$End of line/string
\ABeginning of string
\ZEnd of string
\bWord boundary
\BNot a word boundary

Example

^\d+$

Matches

12345

But not

abc123

Alternation

Equivalent to OR.

cat|dog

Matches either

cat

or

dog

Groups

Capturing Group

(\d+)

Stores the match.

Example

2026

Group 1

2026

Non-Capturing Group

(?:cat|dog)

Useful when grouping without creating a capture.


Named Groups

Syntax depends on the regex engine.

Python

(?P<year>\d+)

JavaScript

(?<year>\d+)

Backreferences

Reuse a previously captured group.

(\w+)\s+\1

Matches

hello hello

Does not match

hello world

Lookarounds

Lookarounds test context without consuming characters.

Positive Lookahead

\d+(?=)

Matches

50

inside

50€

Negative Lookahead

\d+(?!)

Matches numbers not followed by .


Positive Lookbehind

(?<=\$)\d+

Matches

100

inside

$100

Negative Lookbehind

(?<!\$)\d+

Matches numbers not preceded by $.


Escaping Special Characters

Characters with special meaning must be escaped.

.
+
*
?
[
]
(
)
{
}
\
^
$
|

Example

\.

Matches

.

Flags

Flags modify regex behavior.

FlagMeaning
iIgnore case
gGlobal search
mMultiline
sDot matches newline
uUnicode
xIgnore whitespace/comments (engine dependent)

Example

/hello/gi

Common Patterns

Integer

^-?\d+$

Decimal Number

^-?\d+(\.\d+)?$

Email

^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$

URL (Simple)

https?://\S+

Hex Color

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

IPv4

^(\d{1,3}\.){3}\d{1,3}$

Date (YYYY-MM-DD)

^\d{4}-\d{2}-\d{2}$

Time (HH:MM)

^\d{2}:\d{2}$

Whitespace

\s+

Remove Multiple Spaces

Search

\s+

Replace

(space)

Extract Words

\w+

HTML Tags

<[^>]+>

Regex Engine Differences

Not all regex engines support the same features.

FeatureJSPythonPCRERust
Lookahead
Lookbehind✓ (modern)
Named Groups
Backreferences
Atomic Groups

Always check your language’s regex engine documentation before using advanced features.


Most Useful Tokens

.          Any character
 
\d         Digit
 
\w         Word
 
\s         Whitespace
 
^          Start
 
$          End
 
[]         Character class
 
[^]        Negated class
 
()         Capturing group
 
(?:)       Non-capturing group
 
|          OR
 
*          0+
 
+          1+
 
?          Optional
 
{n}        Exactly n
 
{m,n}      Range
 
\b         Word boundary
 
\1         Backreference
 
(?=)       Lookahead
 
(?! )      Negative lookahead
 
(?<=)      Lookbehind
 
(?<!)      Negative lookbehind

Example

Extract the filename and extension:

Input

archive.tar.gz

Regex

^(.+)\.([^.]+)$

Result

GroupValue
1archive.tar
2gz

This illustrates how groups can capture different parts of a string for later use.