DevBolt
·10 min read

Regex Cheat Sheet: Patterns Every Developer Should Know

RegexReferenceProductivity

Regular expressions are one of those tools that feel cryptic until they click — then you use them everywhere. This cheat sheet covers the syntax you'll actually need, with practical examples for common tasks like validation, parsing, and search-and-replace.

Metacharacters

These characters have special meaning in regex. To match them literally, escape with a backslash (\).

PatternDescriptionExample
.Any character except newlinea.c → abc, a1c
^Start of string (or line in multiline mode)^Hello
$End of string (or line in multiline mode)world$
\Escape next character\. matches literal dot
|Alternation (OR)cat|dog
()Capturing group(abc)+
[]Character class[aeiou]

Character Classes

PatternDescription
[abc]Match a, b, or c
[^abc]Match anything except a, b, or c
[a-z]Match any lowercase letter
[A-Z0-9]Match uppercase letter or digit
\dDigit [0-9]
\DNon-digit [^0-9]
\wWord character [a-zA-Z0-9_]
\WNon-word character
\sWhitespace (space, tab, newline)
\SNon-whitespace

Quantifiers

Quantifiers specify how many times the preceding element should match. By default they are greedy (match as much as possible). Add ? to make them lazy.

PatternDescriptionExample
*0 or moreab*c → ac, abc, abbc
+1 or moreab+c → abc, abbc
?0 or 1 (optional)colou?r → color, colour
{n}Exactly n times\d{4} → 2026
{n,}n or more times\d{2,} → 42, 123
{n,m}Between n and m times\d{2,4} → 42, 123, 2026
*?0 or more (lazy)<.*?> → first tag only
+?1 or more (lazy)

Anchors & Boundaries

PatternDescriptionExample
^Start of string^Start
$End of stringend$
\bWord boundary\bword\b
\BNot a word boundary

Groups & Backreferences

PatternDescriptionExample
(abc)Capturing group(\d+)-(\d+)
(?:abc)Non-capturing group
(?<name>abc)Named capturing group
\1Backreference to group 1(\w+)\s\1 → word word
(?=abc)Lookahead (matches if followed by abc)
(?!abc)Negative lookahead
(?<=abc)Lookbehind (matches if preceded by abc)
(?<!abc)Negative lookbehind

Flags

PatternDescription
gGlobal — find all matches, not just the first
iCase-insensitive matching
mMultiline — ^ and $ match line boundaries
sDotall — . matches newline characters
uUnicode — correct handling of Unicode characters

Common Patterns

Copy-paste ready patterns for everyday validation and parsing tasks:

Email (simplified)

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

URL

https?:\/\/[\w\-]+(\.[\w\-]+)+[\/\w\-._~:?#[\]@!$&'()*+,;=]*

IPv4 Address

^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$

Date (YYYY-MM-DD)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Hex Color

^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$

Strong Password

At least 8 characters, one uppercase, one lowercase, one digit, one special character:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

HTML Tags

<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>(.*?)<\/\1>

Tips for Writing Better Regex

  • Start simple — get a basic pattern working first, then refine edge cases.
  • Use non-capturing groups ((?:...)) when you don't need the match — it's faster and cleaner.
  • Be specific \d{4} is better than \d+ when you know you need exactly 4 digits.
  • Test with edge cases — empty strings, special characters, and Unicode input.
  • Avoid catastrophic backtracking — nested quantifiers like (a+)+ can freeze your program on bad input.

Test Your Patterns

Use our Regex Tester to try these patterns with real-time highlighting. Paste any pattern from this guide and test it against your own input — everything runs locally in your browser.