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.
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.
| Token | Matches | Example |
|---|---|---|
. | Any character except a newline | a.c matches abc, a7c |
\d | A digit (0–9) | \d\d matches 42 |
\D | Any character that is not a digit | \D matches x, , - |
\w | A word character: letter, digit or _ | \w+ matches user_1 |
\W | Any non-word character | \W matches !, , . |
\s | Any whitespace (space, tab, newline) | a\sb matches a b |
\S | Any 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.
| Token | Matches | Example |
|---|---|---|
^ | 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 |
\b | A word boundary (edge of a \w run) | \bcat\b matches cat but not category |
\B | A 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).
| Token | Matches | Example |
|---|---|---|
* | Zero or more of the preceding token | ab* matches a, ab, abbb |
+ | One or more of the preceding token | ab+ 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.
| Token | Matches | Example |
|---|---|---|
(...) | 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 side | cat|dog matches cat or dog |
\1 | A 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.
| Token | Matches | Example |
|---|---|---|
(?=...) | 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.
| Flag | Effect |
|---|---|
g | Global — find all matches, not just the first |
i | Case-insensitive — a also matches A |
m | Multiline — ^ and $ match at line breaks |
s | Dotall — . also matches newline characters |
Handy examples
A few patterns that put the tokens above to work:
| Goal | Pattern | Notes |
|---|---|---|
| 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\b | Avoids 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.