Regex syntax
Every regex token you'll actually use, translated to one plain-English line — classes, quantifiers, anchors, groups and flags in one printable sheet.
Last updated
Character classes
| Token | Meaning |
|---|---|
| . | Any single character except a newline (the s flag adds newlines). |
| \d \D | A digit 0–9 / anything that isn't one. |
| \w \W | A word character (letter, digit, underscore) / anything else. |
| \s \S | Any whitespace (space, tab, newline) / anything that isn't. |
| [abc] | Exactly one character from the set: a, b or c. |
| [^abc] | One character that is NOT a, b or c — ^ negates only inside brackets. |
| [a-z0-9] | One character from either range; ranges and singles mix freely. |
Quantifiers
| Token | Meaning |
|---|---|
| * | The previous item, zero or more times. |
| + | The previous item, one or more times. |
| ? | The previous item, optional (zero or one). |
| {3} {2,5} {2,} | Exactly 3 / between 2 and 5 / at least 2 repetitions. |
| *? +? ?? | The lazy versions: match as LITTLE as possible instead of as much. |
Anchors & boundaries
| Token | Meaning |
|---|---|
| ^ $ | Start / end of the string (of each line, with the m flag). |
| \b | A word boundary — the zero-width seam between a word character and a non-word one. |
| \B | The opposite: a position that is NOT a word boundary. |
Groups & alternation
| Token | Meaning |
|---|---|
| (cat|dog) | Either alternative, captured as group 1. |
| (?:cat|dog) | Same choice, but not captured — cheaper and keeps numbering clean. |
| (?<name>…) | A named capture, read back as \k<name> or in match.groups.name. |
| \1 | Whatever group 1 actually matched, repeated here (a backreference). |
| (?=…) (?!…) | Lookahead: the next text must / must not match — without consuming it. |
| (?<=…) (?<!…) | Lookbehind: the preceding text must / must not match. |
Flags
| Token | Meaning |
|---|---|
| g | Global — find every match, not just the first. |
| i | Case-insensitive matching. |
| m | Multiline — ^ and $ also match at line breaks. |
| s | Dotall — . matches newlines too. |
| u | Unicode mode — treat the pattern as code points; needed for \p{…}. |
Worth remembering
- →To match a special character literally, escape it: \. \+ \( \[ — forgetting this on the dot is the most common regex bug in the wild.
- →Every token here is testable live in the Regex Tester — paste a pattern and your own text, nothing leaves the browser.
Questions
What is the difference between greedy and lazy?
Greedy quantifiers take as much as possible and give back only when forced; adding ? makes them lazy, taking as little as possible. It is the difference between matching one HTML tag and matching from the first to the last.
When do I need to escape a character?
For the metacharacters . ^ $ * + ? ( ) [ ] { } | and the backslash itself. Inside a character class most lose their meaning, so only the caret, hyphen and closing bracket need care there.
Why is my pattern slow on long input?
Usually catastrophic backtracking, where nested quantifiers make the engine try exponentially many combinations. Anything of the form (a+)+ on non-matching input can hang a process outright.
What is a non-capturing group for?
Grouping for a quantifier or an alternation without adding an entry to the results. Using (?: ) rather than ( ) keeps the match array to the things you actually want to read.
Do these work the same in every language?
The basics do. Lookbehind, named groups, unicode properties and the exact meaning of \b vary between engines, so a pattern moved from one language to another is worth retesting rather than assuming.