Regex Cheatsheet

Regex syntax with examples, patterns to adapt, and what differs between engines.

By BoltQuickTools team · Updated November 8, 2026

Syntax here is common to most engines unless noted. Try patterns in the regex tester, which uses your browser's JavaScript engine, so patterns specific to Python or PCRE may behave differently there. For a conceptual walkthrough, see the regex guide.

Character Classes

PatternMatchesExample
.Any character except a line break (unless the s flag is on)a.c matches "abc", "aXc"
\dDigit\d{3} matches "123"
\DNon-digit\D+ matches "abc!"
\wWord character: letter, digit, underscore\w+ matches "hello_123"
\WNon-word character\W matches a space or "!"
\sWhitespace, including tabs and line breaks\s+ matches " "
\SNon-whitespace\S+ matches "word"
[abc]Any one of a, b, c[aeiou] matches a vowel
[^abc]Any one except a, b, c[^0-9] matches a non-digit
[a-z]Range a through z[A-Za-z0-9] alphanumeric
\t \n \rTab, line feed, carriage return\r?\n matches either line ending
\xHH \uHHHHCharacter by hex code (\u support varies)\x41 matches "A"
\p{L} \p{Lu}Unicode property: any letter, uppercase letter (see engine table)\p{L}+ matches "café"

\d and \w are ASCII-only in JavaScript, Go and Java by default. In Python 3 string patterns and in .NET they also match non-ASCII digits and letters. Use [0-9] when you need exactly the ASCII digits.

Quantifiers

PatternMatchesNotes
*0 or moreGreedy: takes as much as it can, then gives back
+1 or moreGreedy
?0 or 1After another quantifier, makes it lazy
{n}Exactly n\d{3}
{n,}n or more\d{3,}
{n,m}Between n and m\d{3,5}
*? +? ?? {n,m}?Lazy versions: as few as possibleChanges which match is found first. Lazy patterns still backtrack.
*+ ++ ?+Possessive: never give backSee engine table

Anchors and Boundaries

PatternMatchesExample
^Start of string, or of each line with the m flag^hello
$End of string, or of each line with the m flag. In Python, PCRE and Java it can also match just before a final newline.world$
\A \zAbsolute start and end of string (not JavaScript; Python 3 used \Z for the end before adding \z)\Aid=\d+\z
\bWord boundary\bcat\b matches "cat" but not inside "category"
\BNot a word boundary\Bcat matches the "cat" in "scattered"

Groups and References

PatternMatchesExample
(abc)Capturing group(\d{3})-(\d{4}) captures two parts
(?:abc)Group without capturing(?:ab)+ matches "abab"
(?<name>abc)Named group. Python's re uses (?P<name>abc)Refer back with \k<name>
\1, \2Backreference to a group(a)\1 matches "aa"
|Alternation. Lowest precedence, so group it to limit its reach.^(?:cat|dog)$
(?>abc)Atomic group: no backtracking into itSee engine table
$1 or \1In replacement strings. JavaScript and .NET use $1; Python uses \1Swap: (\w+) (\w+) to $2 $1

Lookarounds

PatternMatchesExample
(?=abc)Followed by abc\d+(?=px) gets "100" from "100px"
(?!abc)Not followed by abc\d+(?!\d|px) skips "100px"
(?<=abc)Preceded by abc(?<=\$)\d+ gets "20" from "$20"
(?<!abc)Not preceded by abc(?<!\$)\b\d+ skips "$20"

Lookarounds consume nothing, so a greedy quantifier can step back to satisfy one. \d+(?!px) on "100px" matches "10", because the next character is then "0", which is not "px". Adding \d to the lookahead, as in the table, stops that.

Flags

FlagEffect
iCase-insensitive
gFind all matches, not just the first (JavaScript, PCRE tools). Python uses findall or finditer instead.
m^ and $ match at line boundaries
s. also matches line breaks
uUnicode mode (JavaScript); required for \p{...}
vJavaScript's newer Unicode mode, adding set operations in classes
ySticky: match only at lastIndex (JavaScript)
dInclude capture start and end positions (JavaScript)
xIgnore whitespace in the pattern and allow comments (not JavaScript)

Engine Differences

FeatureJavaScriptPython rePCREJava.NETGo
LookaheadYesYesYesYesYesNo
LookbehindYes, any lengthFixed widthLimited lengthBounded lengthYes, any lengthNo
BackreferencesYesYesYesYesYesNo
Atomic groupsNo3.11+YesYesYesNo
PossessiveNo3.11+YesYesNoNo
\p{...}With u or vNo (third-party regex module has it)YesYesYesYes
Named groups(?<n>)(?P<n>)Both(?<n>)(?<n>)(?P<n>), (?<n>) since 1.22

Go's RE2 engine gives up backreferences and lookaround in exchange for matching time that grows linearly with input, so it cannot suffer catastrophic backtracking.

Patterns to Adapt

These check shape, not truth. A pattern can say a date looks right, but not that it exists.

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Email, loosely. Stricter than the RFCs in some ways and looser in others. To confirm an address, send it a message.
^https?:\/\/[^\s/$.?#].[^\s]*$
HTTP or HTTPS URL. Prefer your language's URL parser when you need the parts.
^\+[1-9]\d{1,14}$
Phone number in E.164 form: plus sign and up to 15 digits. Says nothing about whether the number is assigned.
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Date YYYY-MM-DD. Accepts impossible dates such as 2026-02-31.
^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$
Time HH:MM or HH:MM:SS, 24-hour, two-digit hours.
^#(?:[a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$
CSS hex color, with optional alpha: #abc, #abcd, #aabbcc, #aabbccdd.
^(?:[a-zA-Z0-9+\/]{4})*(?:[a-zA-Z0-9+\/]{2}==|[a-zA-Z0-9+\/]{3}=)?$
Standard Base64 with padding. Also matches the empty string.
^(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)$
IPv4 address with each octet 0 to 255.
^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$
UUID of any version.
^[a-z0-9]+(-[a-z0-9]+)*$
URL slug: lowercase with single hyphens.
(?<=\$)\d+(\.\d{2})?
Amount after a dollar sign. Does not handle thousands separators.
\b[A-Z][a-z0-9]*(?:[A-Z][a-z0-9]*)+\b
PascalCase word with at least two humps.
\b[a-z]+(?:[A-Z][a-z0-9]*)+\b
camelCase word with at least one hump.
^\s+|\s+$
Leading or trailing whitespace. Replace with nothing using the g flag. Add m to trim every line.
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{12,}$
Stacked lookaheads: requires lowercase, uppercase, digit and 12 or more characters. Shows the technique; length matters more than character rules for passwords.

Common Gotchas

Last updated