🧩
← Back to Guides

20 Common Regex Patterns Every Developer Should Know

· Tags: common-regex-patterns, regex-examples, useful-regex, email-regex, url-regex, phone-regex

20 Common Regex Patterns Every Developer Should Know

Regex solves real problems: input validation, data extraction, and string transformation. This guide presents 20 ready-to-use regular expressions covering the most common text-processing tasks. Each pattern includes an explanation of what it matches.

1. Email Address

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

Local part allows letters, digits, dots, and special chars. Domain allows letters, digits, and dots. TLD must be at least two letters.

2. URL

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z]{2,}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

Matches HTTP/HTTPS URLs with optional www, domain name, TLD, path, and query string.

3. US Phone Number

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

Matches (555) 123-4567, 555-123-4567, 555.123.4567, and 5551234567.

4. International Phone

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

Permissive pattern for international numbers with country codes.

5. Strong Password

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

Requires 8+ chars, one lowercase, one uppercase, one digit, one special char. Uses lookahead assertions.

6. Date (YYYY-MM-DD)

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

ISO 8601 format. Month 01-12, day 01-31. Does not validate per-month limits (Feb 30 passes).

7. Date (MM/DD/YYYY)

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

US format with slashes.

8. Time (HH:MM 24-Hour)

^([01]\d|2[0-3]):[0-5]\d$

Hours 00-23, minutes 00-59.

9. IPv4 Address

^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$

Four octets. For strict validation (0-255), replace \d{1,3} with (25[0-5]|2[0-4]\d|[01]?\d\d?).

10. IPv6 Address

^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$

Full IPv6 with 8 groups of hex digits. Does not handle :: abbreviation.

11. MAC Address

^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$

Colon or hyphen separated.

12. Hex Color Code

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

3-digit (#fff) or 6-digit (#ffffff), case-insensitive.

13. Credit Card Number

^\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}$

16 digits with optional separators. Does not validate Luhn checksum.

14. US ZIP Code

^\d{5}(-\d{4})?$

12345 or 12345-6789.

15. Username

^[a-zA-Z0-9_]{3,16}$

3-16 characters, letters, digits, underscores only.

16. URL Slug

^[a-z0-9-]+$

Lowercase letters, digits, and hyphens. Common in CMS systems.

17. HTML Tag

<\/?[\w\s]*>|<.+[\w\s]*>

Matches opening and closing tags. Not suitable for nested HTML.

18. File Extension

\.([a-zA-Z]{2,})$

Extracts extension without the dot.

19. Whitespace Normalization

\s+

Collapses multiple spaces, tabs, or newlines into one.

20. Extract Numbers

-?\d+(\.\d+)?

Integers and decimals, including negatives.

Performance Tips

  • Be specific — avoid .* which can cause catastrophic backtracking.
  • Anchor with ^ and $ to limit search range.
  • Test edge cases: empty strings, long strings, special characters.

Test These Patterns Online

Use the regex tester tool to experiment with these patterns. Paste any expression, enter sample text, and see matches highlighted in real time. The tester shows capture groups and helps debug patterns before use.