Skip to content
Everyone-Website
Cheat sheet

Regex Cheat Sheet: Common Patterns & Tokens

A quick reference for regular expressions — character classes, anchors, quantifiers, groups, lookarounds and flags — with clear examples you can copy.

Updated 2 June 2026 5 min read

A regular expression (regex) is a compact pattern that describes a set of strings, used to search, match, validate or replace text. This cheat sheet collects the tokens you’ll reach for most often — grouped by job — with short examples you can copy and adapt.

Character classes

Character classes match a single character from a set. Use [...] to build your own, or the shorthand classes for common groups.

TokenMatchesExample
.Any character except a newlinea.c matches abc, a7c
\dA digit (09)\d\d matches 42
\DAny character that is not a digit\D matches x, , -
\wA word character: letter, digit or _\w+ matches user_1
\WAny non-word character\W matches !, , .
\sAny whitespace (space, tab, newline)a\sb matches a b
\SAny non-whitespace character\S+ matches hello
[abc]Any one of a, b or c[abc] matches b
[^abc]Any character except a, b or c[^abc] matches d
[a-z]Any character in the range a to z[a-z] matches m

Tip: ranges and single characters combine inside one class, so [A-Za-z0-9_] is the same as \w.

Anchors & boundaries

Anchors don’t match characters — they match a position in the string, such as the start, the end, or the edge of a word.

TokenMatchesExample
^Start of the string (or line with the m flag)^Hi matches Hi there
$End of the string (or line with the m flag)end$ matches the end
\bA word boundary (edge of a \w run)\bcat\b matches cat but not category
\BA non-word boundary (not at an edge)\Bcat\B matches cat inside locating

Quantifiers

Quantifiers say how many times the preceding token may repeat. By default they are greedy (match as much as possible); add ? to make them lazy (match as little as possible).

TokenMatchesExample
*Zero or more of the preceding tokenab* matches a, ab, abbb
+One or more of the preceding tokenab+ matches ab, abbb (not a)
?Zero or one (makes it optional)colou?r matches color and colour
{n}Exactly n times\d{4} matches 2026
{n,}n or more times\d{2,} matches 42, 4200
{n,m}Between n and m times\d{2,4} matches 42, 4200
*?Lazy * — as few as possible<.*?> matches each <tag> separately

Note: +?, ?? and {n,m}? are the lazy versions of the other quantifiers and work the same way.

Groups & alternation

Groups bundle tokens together so a quantifier or alternation applies to the whole bundle. Capturing groups also remember what they matched for reuse or extraction.

TokenMatchesExample
(...)A capturing group (remembers the match)(ab)+ matches abab
(?:...)A non-capturing group (groups without storing)(?:ab)+ groups but captures nothing
|Alternation — either the left or the right sidecat|dog matches cat or dog
\1A backreference to capturing group 1(\w)\1 matches a doubled letter like oo

Tip: combine grouping and alternation, as in gr(a\|e)y, to match gray or grey.

Lookarounds

Lookarounds assert that text does (or does not) come before or after the current position, without including that text in the match.

TokenMatchesExample
(?=...)Positive lookahead — followed by ...\d+(?= USD) matches 100 in 100 USD
(?!...)Negative lookahead — not followed by ...\d+(?! USD) matches 100 not before USD
(?<=...)Positive lookbehind — preceded by ...(?<=\$)\d+ matches 100 in $100
(?<!...)Negative lookbehind — not preceded by ...(?<!\$)\d+ matches 100 not after $

Flags

Flags (also called modifiers) change how the whole pattern is applied. In JavaScript they follow the closing slash, as in /pattern/gi.

FlagEffect
gGlobal — find all matches, not just the first
iCase-insensitive — a also matches A
mMultiline — ^ and $ match at line breaks
sDotall — . also matches newline characters

Handy examples

A few patterns that put the tokens above to work:

GoalPatternNotes
Digits only (whole string)^\d+$Match a string of one or more digits
Simple email-like^[\w.+-]+@[\w-]+\.[\w.-]+$A loose check, not full RFC validation
Trim leading/trailing spaces^\s+|\s+$Replace matches with an empty string
Hex colour code^#[0-9a-fA-F]{6}$Matches #1a2b3c
Whole word “error”\berror\bAvoids matching errors or terror

Exact syntax varies by regex flavor — JavaScript, PCRE (PHP/Perl), Python, Java, .NET and grep differ in details such as lookbehind support, named groups and flag placement — so test your pattern in the engine you’re targeting.