Regex Cheatsheet - Common Patterns & Syntax Reference
The usual regex pain point is not remembering metacharacters, but whether to reach for ? or *, whether to use lookahead or lookbehind, and whether your pattern is greedy or lazy. This reference groups character classes, quantifiers, capture groups and these assertions separately, each with an explanation and a real-world template. When validating a form or filtering logs, find the group that matches your target, then adjust the boundaries against the template.
Character Classes 10
[abc]Match any one of a, b, or c
[^abc]Match any char except a, b, c
[a-z]Match any lowercase letter a-z
\dDigit, equivalent to [0-9]
\DNon-digit
\wWord char (letter, digit, underscore); [A-Za-z0-9_]
\WNon-word char
\sWhitespace (space, tab, newline)
\SNon-whitespace
.Any single char except newline
Anchors 8
^Start of string
$End of string
\bWord boundary
\BNon-word boundary
(?=...)Positive lookahead: must be followed by ... but not consumed
(?!...)Negative lookahead: must not be followed by ...
(?<=...)Positive lookbehind: must be preceded by ...
(?<!...)Negative lookbehind: must not be preceded by ...
Quantifiers 8
*0 or more
+1 or more
?0 or 1
{n}Exactly n
{n,}At least n
{n,m}Between n and m
*?Lazy: match as few as possible (default is greedy)
+?Lazy 1 or more
Groups & Capturing 6
(abc)Capturing group; result index 1
(?:abc)Non-capturing group (group only, no capture)
(?<name>abc)Named capturing group
a|bMatch a or b
\1Backreference to group 1
\k<name>Backreference to a named group
Escaping & Flags 7
\Match a literal backslash
\.Match a literal dot (escape a metachar)
/i/i - ignore case
/g/g - global match (find all)
/m/m - multiline; ^ $ match each line
/s/s - dotAll; . also matches newline
/u/u - Unicode mode
Practical Patterns 5
[\w.+-]+@[\w-]+\.[\w.-]+Email
https?://[\w./?=&#%-]+URL (http/https)
1[3-9]\d{9}Mainland China mobile number
^\d{4}-\d{2}-\d{2}$YYYY-MM-DD date
^[#\w-]{6,}$Password: at least 6 chars (letters, digits, underscore, hyphen)
Common Pitfalls 4
Greedy vs LazyGreedy vs Lazy
Dot doesn't match newlineDot doesn't match newline
Escape special charactersEscape special characters
catastrophic backtrackingCatastrophic backtracking
Tips
- Quantifiers are greedy by default; add ? for lazy (e.g. <.*?> matches the shortest).
- Dot . doesn't match newline; use /s flag or [\s\S] for cross-line.
- Metachars . ^ $ * + ? ( ) [ ] { } | \ / must be escaped with \ when used literally.
- Avoid nested quantifiers like (.+)+; on long strings they can cause exponential backtracking.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Aug 2, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us