Regex Quick Reference

Your one-page cheat sheet for Regular Expression syntax. Print it, bookmark it, keep it open in another tab. Fast lookup when you need it most.

Metacharacters
.Any character (except newline)^Start of string/line$End of string/line|Alternation (OR)\Escape special character
Quantifiers
*0 or more+1 or more?0 or 1 (optional){n}Exactly n times{n,}n or more times{n,m}Between n and m times*?Lazy (non-greedy) version
Character Classes
[abc]a, b, or c[^abc]NOT a, b, or c[a-z]Range: a through z\dDigit [0-9]\DNon-digit [^0-9]\wWord char [a-zA-Z0-9_]\WNon-word char\sWhitespace\SNon-whitespace
Anchors & Boundaries
^Start of string$End of string\bWord boundary\BNon-word boundary
Groups & Capturing
(abc)Capture group(?:abc)Non-capturing group(?<name>)Named group\1Backreference to group 1$1Group 1 in replacement
Lookaround
(?=abc)Positive lookahead(?!abc)Negative lookahead(?<=abc)Positive lookbehind(?<!abc)Negative lookbehind
Common Flags
gGlobal - all matchesiCase insensitivemMultiline modesDotall (. matches newline)uUnicode mode
Common Patterns
Email: ^\S+@\S+\.\S+$
URL: https?://\S+
Phone: \d{3}-\d{3}-\d{4}
Date: \d{4}-\d{2}-\d{2}
IP: \d{1,3}(\.\d{1,3}){3}

Regex Quick Reference: Everything You Need at a Glance

This quick reference distills the most commonly used regular expression syntax into a scannable format. Whether you are writing validation logic, parsing log files, or performing sophisticated text transformations, these are the building blocks you will reach for repeatedly. Keep this page bookmarked for those moments when you need to quickly look up the syntax for a quantifier or the format of a lookahead assertion.

Regular expressions have a reputation for being write-only—easy to write in the moment but cryptic to read later. The key to maintainable regex is understanding each component. When you know what each symbol means, even complex patterns become readable as a sequence of logical steps.

Reading Regex: A Mental Framework

Approach reading a regex from left to right, breaking it down into components. Identify the main structure first: are there alternations (|)? Are there groups? Then look at each segment: what characters does it match, and how many times (quantifiers)?

For example, a URL regex looks intimidating at first glance. But break it down piece by piece: the caret anchors to start; an optional protocol group matches http or https; a character class group matches the domain name; another group matches the TLD; and the rest handles paths.

When writing regex, build incrementally. Start with the simplest pattern that matches your target, then add complexity to exclude false positives. Test with both matching and non-matching inputs at each step.

Character Classes Demystified

Character classes define sets of characters that can match at a single position. Square brackets create custom classes: [aeiou] matches any vowel, [0-9a-fA-F] matches hexadecimal digits.

Inside character classes, most metacharacters lose their special meaning. [.] matches a literal period—no escaping needed. The exceptions are ^ (negation when first), - (range when between characters), ] (closes the class), and \ (still escapes).

The shorthand classes \d, \w, \s and their negations are conveniences for common patterns. \d is equivalent to [0-9], \w to [a-zA-Z0-9_], \s to [ \t\n\r\f] (whitespace characters).

Quantifier Nuances

Quantifiers specify repetition, but their greedy/lazy behavior matters enormously. Greedy quantifiers (the default) match as much as possible while still allowing the overall pattern to succeed. a.*b on "aXbYb" matches the entire string, not just "aXb".

Lazy quantifiers (with ? suffix) match as little as possible. a.*?b on "aXbYb" matches just "aXb". Choose based on whether you want the shortest or longest possible match.

Possessive quantifiers (*+, ++) match like greedy but never backtrack—once matched, those characters are committed. This can improve performance but may cause patterns to fail to match where greedy would succeed.

The Power of Lookaround

Lookaround assertions are zero-width—they match a position based on what is around it without including those characters in the match. This enables patterns that would otherwise require multiple passes or complex logic.

Positive lookahead (?=...) asserts that what follows matches the pattern. password(?=.*\d)(?=.*[A-Z]) matches "password" only if followed somewhere by both a digit and an uppercase letter—useful for password validation.

Negative lookahead (?!...) is equally powerful. foo(?!bar) matches "foo" except when followed by "bar". This is useful for excluding specific cases from a broader pattern.

Lookbehind works similarly but checks what precedes. (?<=$)\d+ matches digits preceded by a dollar sign. Note that lookbehind has restrictions in some regex engines—JavaScript requires fixed-length patterns in lookbehind.

Groups: More Than Just Capturing

Parentheses create groups that serve multiple purposes. They capture matched content for backreferences and replacement. They define the scope of alternation: gray|grey matches either word, but gr(a|e)y shows the alternatives are just the middle character.

Non-capturing groups (?:...) provide grouping without the overhead of capturing. Use them when you need to group for quantifiers or alternation but do not need to reference the matched content.

Named groups improve readability in complex patterns. Instead of remembering group numbers, use named capture groups and access them by name in your code.

Flags and Their Effects

Flags modify how the entire pattern behaves. The global flag g finds all matches instead of stopping at the first. The case-insensitive flag i makes /abc/i match "ABC", "Abc", etc.

The multiline flag m changes ^ and $ to match line boundaries instead of string boundaries. This is essential for per-line matching in multi-line text.

The dotall flag s makes . match newline characters. Without it, . matches any character except newlines. The unicode flag u enables proper Unicode matching, essential for international text and emoji.

Testing and Debugging

Always test regex with a variety of inputs: strings that should match, strings that should not match, edge cases, empty strings, and strings with special characters. Online tools like regex101.com provide real-time visualization and explanation.

When a pattern does not work as expected, simplify it. Remove parts until you have a minimal pattern that demonstrates the problem. Often the issue becomes obvious when isolated.

For production regex, add comments explaining the purpose. Many languages support verbose mode where you can include whitespace and comments within the pattern itself, significantly improving maintainability.