developmentregexproductivity

Regex Cheat Sheet: 20 Patterns Every Developer Uses

Regular expressions are one of the most powerful tools in a developer's toolkit — and one of the most misunderstood. Here are the 20 patterns you'll reach for over and over, with plain-English explanations.

·8 min read

Why Regex Is Worth Learning

Regular expressions let you match, extract, and replace text patterns with a single line of code. Once you know the core syntax, you'll write less custom string-parsing code and reach for grep, sed, and IDE search-replace with far more precision.

The Core Syntax in 2 Minutes

  • . — any single character (except newline)
  • * — zero or more of the previous
  • + — one or more of the previous
  • ? — zero or one (makes preceding optional)
  • ^ — start of string (or line in multiline mode)
  • $ — end of string (or line in multiline mode)
  • [abc] — character class: a, b, or c
  • [^abc] — negated class: anything except a, b, or c
  • (group) — capturing group
  • (?:group) — non-capturing group
  • a|b — alternation: a or b
  • \d — digit [0-9]
  • \w — word character [a-zA-Z0-9_]
  • \s — whitespace
  • {n,m} — between n and m repetitions

The 20 Patterns

**1. Email address** [a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}

**2. URL (http/https)** https?://[^\s<>"{}|\\^]+

**3. IPv4 address** \b(?:\d{1,3}\.){3}\d{1,3}\b

**4. IPv6 address (compressed)** ([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}

**5. Hex color code** #(?:[0-9a-fA-F]{3}){1,2}\b

**6. US phone number** (?:\+1)?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}

**7. Date (YYYY-MM-DD)** \b\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])\b

**8. Time (HH:MM or HH:MM:SS)** \b([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?\b

**9. Credit card number** \b(?:\d{4}[\s-]?){3}\d{4}\b

**10. ZIP / postal code (US)** \b\d{5}(?:-\d{4})?\b

**11. Semantic version** \bv?\d+\.\d+\.\d+(?:-[\w.]+)?\b

**12. HTML tag** <[^>]+>

**13. HTML comment** <!--[\s\S]*?-->

**14. CSS class** \.([a-zA-Z_][\w-]*)

**15. JavaScript variable name** \b(?:const|let|var)\s+([a-zA-Z_$][\w$]*)

**16. Markdown heading** ^#{1,6}\s+(.+)$ (use multiline flag)

**17. JSON string value** "([^"\\]|\\.)*"

**18. Dollar amount** \$\d{1,3}(?:,\d{3})*(?:\.\d{2})?

**19. Log level** \b(DEBUG|INFO|WARN(?:ING)?|ERROR|CRITICAL|FATAL)\b

**20. UUID v4** [0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}

Testing Patterns Safely

Paste any pattern into NoxaKit's Regex Tester to see live match highlighting as you type. The Regex Extractor lets you run a pattern against a block of text and pull out all matches — useful for data extraction tasks without writing Python or JavaScript.

Common Mistakes

**Forgetting to escape dots** — . in a character class doesn't need escaping, but outside it matches *any* character. Write \. when you mean a literal dot.

**Greedy vs. lazy** — .* matches as much as possible; .*? matches as little as possible. This matters inside HTML tags: <.*> matches the entire line; <.*?> matches one tag.

**Anchoring** — Without ^ and $, a pattern matches anywhere in the string. Add anchors when you want to validate a whole value, not just find it inside a longer string.

Try These Free Tools

More Articles