Regex cheat sheet

A quick reference for regular expression syntax, plus patterns you can copy for common jobs such as checking an email address or a date. Search the list or click any entry to copy it.

Click any entry to copy it.

Characters

PatternWhat it does
.Any character except a line break
\dA digit, 0 to 9
\DAnything that is not a digit
\wA word character: letter, digit or underscore
\WAnything that is not a word character
\sWhitespace: space, tab or line break
\SAnything that is not whitespace
\tA tab
\nA line break
\.A literal dot. Put a backslash before any special character to match it as text
\\A literal backslash

Character sets

PatternWhat it does
[abc]One of a, b or c
[^abc]Any character except a, b or c
[a-z]Any lowercase letter from a to z
[A-Za-z0-9]Any letter or digit
[a-z-]A lowercase letter or a hyphen. Put the hyphen last to match it as text
\p{L}Any letter in any language (needs the u flag in JavaScript)

Anchors and boundaries

PatternWhat it does
^Start of the text, or of each line with the m flag
$End of the text, or of each line with the m flag
\bWord boundary: the edge between a word character and anything else
\BNot a word boundary

Quantifiers

PatternWhat it does
a*Zero or more of a
a+One or more of a
a?Zero or one of a (optional)
a{3}Exactly three of a
a{2,}Two or more of a
a{2,5}Between two and five of a
a+?One or more, as few as possible (lazy). Works after any quantifier
.*?Any text, as little as possible. Useful between two markers

Groups and alternatives

PatternWhat it does
(abc)Capturing group. Matches abc and remembers it as group 1
(?:abc)Group without capturing
(?<name>abc)Named capturing group
a|bEither a or b
\1The same text group 1 matched (backreference)
\k<name>The same text the named group matched

Lookarounds

PatternWhat it does
a(?=b)a only when followed by b (lookahead)
a(?!b)a only when not followed by b (negative lookahead)
(?<=b)aa only when preceded by b (lookbehind)
(?<!b)aa only when not preceded by b (negative lookbehind)

Flags

PatternWhat it does
gGlobal: find every match, not just the first
iIgnore case
mMultiline: ^ and $ match at each line
sDot matches line breaks too
uUnicode: enables \p{...} and treats emoji as one character
ySticky: match only at the current position

Replacement text

PatternWhat it does
$1Insert what group 1 matched
$<name>Insert what the named group matched
$&Insert the whole match
$$Insert a literal dollar sign

Ready-made patterns

PatternWhat it does
^\d+$Whole number
^-?\d+(\.\d+)?$Number with optional minus sign and decimals
^[^\s@]+@[^\s@]+\.[^\s@]+$Email address (simple check)
^https?:\/\/[^\s]+$Web address starting with http or https
^\d{4}-\d{2}-\d{2}$Date in the form 2026-01-31
^([01]\d|2[0-3]):[0-5]\d$Time in 24-hour form, 00:00 to 23:59
^\d{5}(-\d{4})?$US ZIP code, with optional four-digit extension
^\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}$US phone number
^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$Hex color such as #2563eb
^(25[0-5]|2[0-4]\d|1?\d?\d)(\.(25[0-5]|2[0-4]\d|1?\d?\d)){3}$IPv4 address
^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$UUID (use with the i flag)
^[a-z0-9]+(-[a-z0-9]+)*$URL slug: lowercase words joined by hyphens
^\s+|\s+$Spaces at the start or end (replace with nothing to trim)
\s{2,}Two or more spaces in a row
<[^>]+>An HTML tag
^\s*$An empty or blank line

How to use the cheat sheet

  1. Type what you are looking for, such as digit, optional or email.
  2. Click a pattern to copy it.
  3. Paste it into your code or try it in the regex tester.

How to read a regular expression

A regular expression is a search pattern. Most characters match themselves, so cat finds the letters c, a, t in a row. A few characters have special meaning: . matches any character, * and + repeat the thing before them, and square brackets list the characters allowed in one position. Read a pattern from left to right, one piece at a time.

For example, ^\d{4}-\d{2}-\d{2}$ reads as: start of text, four digits, a hyphen, two digits, a hyphen, two digits, end of text. That matches a date such as 2026-01-31 and nothing else.

Greedy and lazy matching

Quantifiers are greedy: they take as much text as they can. On <b>one</b> and <b>two</b>, the pattern <b>.*</b> matches the whole line. Add a question mark to make it lazy, <b>.*?</b>, and it stops at the first closing tag.

Characters you need to escape

To match one of these as plain text, put a backslash in front of it: . * + ? ^ $ ( ) [ ] { } | \ /. Inside square brackets only ], \, ^ and - need care.

Differences between languages

This sheet follows JavaScript, and nearly all of it works the same in Python, Java, C#, Go, PHP and Rust. The main differences: Go and Rust have no lookarounds or backreferences, Python writes named groups as (?P<name>...), and tools such as grep need the -E or -P option for the modern syntax.

Test a pattern against your own text in the regex tester. Working in the terminal? See the Git cheat sheet.

Frequently asked questions

What does \d mean in regex?

It matches one digit from 0 to 9. Add a plus sign, \d+, to match one or more digits in a row.

What is the difference between * and +?

The star matches zero or more of the item before it, so the item can be missing. The plus needs at least one.

How do I match a literal dot or question mark?

Put a backslash before it: \. matches a dot and \? matches a question mark.

Is there a perfect regex for email addresses?

No. The simple pattern on this page catches typing mistakes, which is what most forms need. The only real test of an address is sending mail to it.

Do these patterns work in Python and other languages?

Yes, with small exceptions that are listed in the section on differences between languages.