Languages
Regex Cheat Sheet — Regular Expression Syntax Reference
Regular expressions are built from a small set of pieces. This reference covers the syntax you actually use day to day, with a short example for each.
Put it into practice with the matching tool.
Test a regex →Advertisement
Character classes
.Any character except newline[abc]Any one of a, b, or c[^abc]Any character except a, b, or c[a-z]Any character in the range a to z\dAny digit (same as [0-9])\DAny non-digit\wWord character: letter, digit, or underscore\WAny non-word character\sAny whitespace (space, tab, newline)\SAny non-whitespace characterAnchors & boundaries
^Start of string (or line with the m flag)$End of string (or line with the m flag)\bWord boundary\BNot a word boundaryQuantifiers
*Zero or more+One or more?Zero or one (optional){3}Exactly three times{2,}Two or more times{2,5}Between two and five times*?Lazy (non-greedy) — match as little as possibleGroups & alternation
(abc)Capturing group(?:abc)Non-capturing group(?<name>abc)Named capturing groupa|bMatch a or b\1Backreference to the first captured groupLookaround
(?=abc)Positive lookahead — followed by abc(?!abc)Negative lookahead — not followed by abc(?<=abc)Positive lookbehind — preceded by abc(?<!abc)Negative lookbehind — not preceded by abcFlags
gGlobal — find all matches, not just the firstiCase-insensitivemMultiline — ^ and $ match line starts/endssDotall — . also matches newlineuUnicode modeCommon patterns
^\S+@\S+\.\S+$Simple email shape^https?:\/\/Starts with http:// or https://^\d{4}-\d{2}-\d{2}$ISO date (YYYY-MM-DD)\b\d{1,3}(\.\d{1,3}){3}\bIPv4 address shape^[a-z0-9-]+$URL slug (lowercase, digits, hyphens)Regex in practice
Reading a pattern from left to right
Take ^\d{3}-\d{4}$. The caret anchors the match to the start, \d{3} is exactly three digits, the hyphen is a literal hyphen, \d{4} is four digits, and the dollar anchors the end. Read any pattern this way: one piece at a time, asking what each piece matches and how many times. Most surprises come from a quantifier applying to less than you expect — ab+ repeats only the b — or from an alternation splitting more than you meant: ^yes|no$ means "starts with yes" or "ends with no".
Mistakes that cause most bugs
Forgetting anchors in validation: \d+ accepts "abc123xyz" because it finds digits somewhere inside. Forgetting to escape a dot: example.com also matches "exampleXcom". Using .* greedily between delimiters: ".*" on "a" and "b" matches the whole span, not just "a"; use "[^"]*" or the lazy ".*?". And assuming \w covers accented letters — in JavaScript it does not, unless you use \p{L} with the u flag.
Escaping inside strings
Most languages write patterns inside string literals, which consume one level of backslashes. In JavaScript, new RegExp("\\d+") needs a doubled backslash, while the literal /\d+/ does not. Python's raw strings, r"\d+", avoid the doubling, which is why Python code nearly always uses them for patterns. When a pattern works in a tester but not in your code, check the escaping first.
Performance and safety
Patterns with nested quantifiers, such as (a+)+ or (\w+\s?)*$, can take exponential time on inputs that almost match — a denial-of-service risk called ReDoS when the input comes from users. Prefer specific character classes over .*, avoid repeating groups that can match the same text in several ways, and set input length limits. Some engines, such as Go's RE2, guarantee linear time by not supporting backreferences and lookarounds.
When not to use a regex
Regular expressions are excellent for matching and extracting patterns in flat text. They are a poor fit for nested formats: use a real parser for HTML, JSON, XML and programming languages. For complicated validation such as full email or URL syntax, a simple sanity check followed by real verification — sending the email, fetching the URL — is more reliable than an enormous pattern nobody can maintain.

