All posts

Regex Cheat Sheet: 15 Practical Examples Every Developer Needs

A practical regex reference with real-world patterns for emails, URLs, IPs, dates, and more — plus a free online regex tester.

Why Regex Still Matters

Regular expressions are one of those tools that feel archaic until you need them. Then they're the fastest way to validate, extract, or transform text. Every developer encounters regex in:

  • Input validation (emails, phone numbers, URLs)
  • Log parsing and search
  • Find-and-replace in codebases
  • Routing patterns in web frameworks
  • Data extraction from unstructured text

15 Patterns You'll Actually Use

1. Email (Simple)

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

Good enough for form validation. Don't try to make a "perfect" email regex — it's impossible per the RFC.

2. URL

https?://[\w.-]+(?:\.[a-zA-Z]{2,})(?:/[^\s]*)?

3. IPv4 Address

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

4. ISO Date (YYYY-MM-DD)

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

5. Phone Number (US)

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

6. Hex Color

#(?:[0-9a-fA-F]{3}){1,2}\b

7. UUID

[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}

8. Semantic Version

\bv?\d+\.\d+\.\d+(?:-[\w.]+)?\b

9. Password Strength (min 8 chars, upper, lower, digit)

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

10. HTML Tags

</?[a-zA-Z][\w]*(?:\s[^>]*)??>

11. Whitespace Cleanup (multiple spaces to one)

\s{2,}

Replace with a single space.

12. JSON Key Extraction

"(\w+)"\s*:

Capture group 1 gives you the key names.

13. Log Timestamp

\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}

14. Environment Variable

\$\{?[A-Z_][A-Z0-9_]*\}?

15. Stack Trace File Reference

(?:at\s+)?[\w./]+:\d+(?::\d+)?

Tips for Writing Better Regex

  1. Start simple, then refine — get a basic match first, then handle edge cases
  2. Use non-capturing groups (?:...) when you don't need the capture
  3. Be specific\d{4} is better than \d+ when you know the length
  4. Test with real data — edge cases always surprise you
  5. Comment complex patterns — use x flag for multiline regex with comments

Test Your Patterns

Use our [free regex tester](/tools/regex-tester) to test patterns with live match highlighting, capture group display, and flag toggles. It runs in your browser using JavaScript's RegExp engine.

Regex in Error Tracking

Bugsly uses regex patterns under the hood for error fingerprinting — grouping similar errors together even when the error message contains variable data like IDs or timestamps. This means you see "500 occurrences" instead of 500 separate issues. [Try it free](/signup).

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free