JosephJ.in

Regex Cheat Sheet — Common Patterns Every Developer Needs

·5 min read

Regular expressions are powerful but notoriously hard to remember. Here’s a practical cheat sheet of patterns you’ll actually use.

Basic Syntax

  • . — any character except newline
  • \d — digit (0-9), \D — non-digit
  • \w — word character (a-z, A-Z, 0-9, _), \W — non-word
  • \s — whitespace, \S — non-whitespace
  • ^ — start of string, $ — end of string
  • \b — word boundary

Quantifiers

  • * — 0 or more
  • + — 1 or more
  • ? — 0 or 1
  • {n} — exactly n times
  • {n,m} — between n and m times
  • {n,} — n or more times

Common Patterns

Email Address

^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$

Matches most standard email formats. For production use, consider a dedicated email validation library.

URL

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

Phone Number (US)

^\+?1?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$

Matches formats like (555) 123-4567, 555.123.4567, +1-555-123-4567.

IP Address (IPv4)

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

Hex Color

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

Date (YYYY-MM-DD)

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

Tips

  • Use non-greedy matching — add ? after quantifiers (.*?) to match the shortest possible string
  • Use named groups(?<name>...) makes captures more readable
  • Use flagsi for case-insensitive, g for global, m for multiline
  • Test incrementally — build your regex piece by piece, testing each part as you go

Try it yourself

Use our free Regex Tester — no signup, no ads interrupting your workflow.

Open Regex Tester