developer reference

Regex cheat sheet

A practical Regex reference with search, one-click copy and printable PDF output.

Go to commands
35
commands
7
categories
0
dependencies

Live regex tester

JS RegExp

This tester uses JavaScript RegExp with global matching (g). Use (?<name>...) for named groups. Python re syntax and flags shown below run in Python, not in this tester. Limits: 100,000 text characters, 4,000 pattern characters, 1,000 matches; slow patterns are stopped.

Match output

35 commands visible

Anchors

4 commands
^

Start of line/string

Anchorsafe
Practical example
^Hello  # matches "Hello world" but not "say Hello"
$

End of line/string

Anchorsafe
Practical example
world$  # matches "Hello world" but not "world class"
\b

Word boundary: word/non-word edge

Anchorsafe
Practical example
\bcat\b  # "cat" but not "catch" or "concat"
\B

Non-word boundary

Anchorsafe
Practical example
\Bcat\B  # "concatenate" but not " cat "

Quantifiers

5 commands
*

0 or more times (greedy)

Quantifierssafe
Practical example
ab*c  # "ac", "abc", "abbc", "abbbc"…
+

1 or more times (greedy)

Quantifierssafe
Practical example
ab+c  # "abc", "abbc" but NOT "ac"
?

0 or 1 time (optional)

Quantifierssafe
Practical example
colou?r  # "color" and "colour"
{n,m}

Between n and m times

Quantifierssafe
Practical example
\d{2,4}  # 2, 3 or 4 digits
\d{3}    # exactly 3 digits
*? +? ??

Lazy versions (non-greedy)

Quantifierssafe
Practical example
# input: "<b>bold</b>"
<.+>   # greedy: "<b>bold</b>"
<.+?>  # lazy: "<b>" then "</b>"

Classes

5 commands
[abc]

Character class: one of the listed characters

Classessafe
Practical example
[aeiou]  # any vowel
[a-z]  # lowercase a-z
[A-Za-z0-9]
[^abc]

Negation: any character except listed ones

Classessafe
Practical example
[^0-9]  # non-digits
\d \w \s

Shorthand: \d digit, \w word, \s whitespace

Classessafe
Practical example
\d  # [0-9]
\w  # [a-zA-Z0-9_]
\s  # [ \t\n\r]
\D \W \S

Negated shorthands

Classessafe
Practical example
\D  # not a digit
\W  # not a word character
\S  # not whitespace
.

Any character except line terminators; the s flag (Python re.S) also matches newlines

Classessafe
Practical example
a.b  # "aXb", "a b", "a1b"…

Groups

6 commands
(...)

Capturing group: captures the match

Groupssafe
Practical example
(\d{4})-(\d{2})-(\d{2})
# matches "2024-01-15", group(1)="2024"
(?:...)

Non-capturing group: groups without capturing

Groupssafe
Practical example
(?:Mr|Mrs|Ms)\.\s\w+
# groups alternatives without capturing
(?<name>...)

Named group: captures with a name

Groupssafe
Practical example
JavaScript: (?<year>\d{4})-(?<month>\d{2})
match.groups.year

Python: (?P<year>\d{4})-(?P<month>\d{2})
m.group("year")
a|b

Alternative: a or b

Groupssafe
Practical example
cat|dog  # "cat" or "dog"
(https?|ftp)://
(?=...) (?!...)

Positive and negative lookahead

Groupssafe
Practical example
\d+(?= euro)  # number followed by " euro" (does not capture " euro")
\d+(?! euro)  # number NOT followed by " euro"
(?<=...) (?<!...)

Positive and negative lookbehind

Groupssafe
Practical example
(?<=\$)\d+  # number preceded by "$"

Python re

6 commands
re.search (pattern, string)

Finds first occurrence anywhere

Python resafe
Practical example
import re
m = re.search(r"\d+", "abc 123 def")
if m:
    print(m.group())  # "123"
re.match (pattern, string)

Matches only at the beginning of the string

Python resafe
Practical example
re.match(r"\d+", "123abc")  # match
re.match(r"\d+", "abc123")  # None
re.findall (pattern, string)

Returns a list of all matches

Python resafe
Practical example
re.findall(r"\d+", "1 apple, 2 bananas, 3 pears")
# → ["1", "2", "3"]
re.sub (pattern, repl, string)

Replaces matches with repl

Python resafe
Practical example
re.sub(r"\s+", " ", "too  many   spaces")
# → "too many spaces"
re.sub(r"(\w+)", r"[\1]", "hello world")
# → "[hello] [world]" (backreference)
re.split (pattern, string)

Splits a string on pattern matches

Python resafe
Practical example
re.split(r"[,;\s]+", "a, b; c  d")
# → ["a", "b", "c", "d"]
re.compile (pattern, flags)

Precompiles a pattern for repeated use

Python resafe
Practical example
EMAIL = re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+")
for line in lines:
    if EMAIL.search(line): ...

Flag

4 commands
re.IGNORECASE

Case-insensitive matching

Flagsafe
Practical example
re.findall(r"hello", text, re.I)
re.compile(r"hello", re.IGNORECASE)
re.MULTILINE

^ and $ match start/end of each line

Flagsafe
Practical example
re.findall(r"^\w+", text, re.M)
re.DOTALL

Dot (.) also matches newline

Flagsafe
Practical example
re.search(r"<div>.+?</div>", html, re.S)
re.VERBOSE

Allows comments in the pattern (re.X)

Flagsafe
Practical example
pattern = re.compile(r"""
    \d{4}  # year
    -
    \d{2}  # month
""", re.X)

Ready-made patterns

5 commands

Email

[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}

Simple email-shaped pattern; not a complete email validator

Ready-made patternssafe
Practical example
[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}

URL

https?://[\w/:%#$&?()~.=+\-]+

Simple HTTP/HTTPS URL pattern; not a complete URL parser

Ready-made patternssafe
Practical example
https?://[\w/:%#$&?()~.=+\-]+

IPv4

\b(?:\d{1,3}\.){3}\d{1,3}\b

IPv4-shaped pattern; does not validate octet ranges (0–255)

Ready-made patternssafe
Practical example
\b(?:\d{1,3}\.){3}\d{1,3}\b

ISO date

\b\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])\b

YYYY-MM-DD-shaped pattern; does not validate calendar dates

Ready-made patternssafe
Practical example
\b\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])\b

Italian phone number

(?:\+39)?[\s-]?(?:0\d{1,4}|3\d{2})[\s-]?\d{6,8}

Simplified Italian phone number pattern; does not confirm a valid or assigned number

Ready-made patternssafe
Practical example
(?:\+39)?[\s-]?(?:0\d{1,4}|3\d{2})[\s-]?\d{6,8}

How to use this Regex cheat sheet

Can I test Python regular expressions here?

The tester runs JavaScript RegExp. Python-specific syntax such as (?P<name>...) and re.VERBOSE must be tested in Python. The ready-made patterns illustrate formats, not complete validation.

Can I print this sheet as a PDF?

Use Print PDF. The printable version includes all commands and practical examples, including sections hidden by a search.

Can I use it locally?

Yes. Serve the static files with a local HTTP server. Search, filters and copying run in your browser; the Regex tester requires a worker-enabled HTTP context.