Regex Cheat Sheet: 20 Essential Patterns Every Developer Needs

Stop Googling the same regex patterns. Here are 20 battle-tested patterns you'll use again and again โ€” copy, paste, and test them live.

๐Ÿงช Test These Patterns Live

Copy any pattern below and test it instantly with your own data.

Open Regex Tester โ†’

Validation Patterns

1. Email Address

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

Matches standard email formats. Covers 99% of real-world addresses. For strict RFC 5322 compliance, the pattern is much more complex โ€” but this handles practical validation.

2. URL (HTTP/HTTPS)

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

Matches HTTP and HTTPS URLs with optional www prefix, domain, and path/query parameters.

3. IPv4 Address

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

Validates proper IPv4 format with each octet between 0-255. Rejects values like 999.999.999.999.

4. IPv6 Address (Simplified)

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

Matches full-form IPv6 addresses (8 groups of hex digits). Does not handle compressed notation (::).

5. Phone Number (International)

^\+?[1-9]\d{1,14}$

E.164 format โ€” the international standard. Optional + prefix, 1-15 digits. Works for any country.

6. US Phone Number

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

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

Data Format Patterns

7. Date (YYYY-MM-DD)

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

ISO 8601 date format. Validates month (01-12) and day (01-31) ranges, though doesn't check month-specific day limits.

8. Time (24-hour HH:MM:SS)

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

24-hour time format. Hours 00-23, minutes and seconds 00-59.

9. Hex Color Code

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

Matches #FFF, #FF5733, and #FF573380 (with alpha). Covers 3, 6, and 8-character hex codes.

10. UUID v4

^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$

Strictly validates UUID version 4 format: the third group starts with 4, the fourth starts with 8, 9, a, or b.

Text Processing Patterns

11. HTML Tags

<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>

Matches paired HTML tags with content. Captures the tag name and inner content. Note: regex isn't ideal for complex HTML parsing โ€” use a DOM parser for production code.

12. Strip HTML Tags

<[^>]+>

Matches any HTML tag for removal. Use with replace to strip all tags from a string.

13. Whitespace Trimming

^\s+|\s+$

Matches leading and trailing whitespace. Use with replace to trim strings.

14. Duplicate Words

\b(\w+)\s+\1\b

Finds repeated consecutive words like "the the" or "is is". Great for proofreading.

Password & Security Patterns

15. Strong Password

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

Requires at least 8 characters with one lowercase, one uppercase, one digit, and one special character. Uses lookaheads for each requirement.

16. Credit Card Number (Basic)

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

Matches 16-digit card numbers with optional dashes or spaces between groups. For real validation, use the Luhn algorithm.

Code & Development Patterns

17. Semantic Version (SemVer)

^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$

Official SemVer regex. Matches versions like 1.0.0, 2.1.3-beta.1, 3.0.0-alpha+build.123.

18. File Extension

\.([a-zA-Z0-9]+)$

Captures the file extension from a filename or path. Group 1 contains the extension without the dot.

19. CSS Property-Value Pair

([a-z-]+)\s*:\s*([^;]+);

Extracts CSS property names and values. Group 1 = property, Group 2 = value.

20. Import/Require Statements

(?:import|require)\s*\(?['"]([^'"]+)['"]\)?

Matches JavaScript/TypeScript import and require statements. Group 1 captures the module path.

How to Use These Patterns

  1. Copy the pattern you need from above
  2. Open our regex tester and paste it in the pattern field
  3. Add test strings โ€” both valid and invalid examples โ€” to verify behavior
  4. Adjust flags (global, case-insensitive, multiline) as needed for your use case
  5. Integrate into your code once you've confirmed it works correctly

Important Caveats

These patterns cover the vast majority of real-world cases, but keep in mind:

Test Any Pattern Instantly

Copy a pattern from above and try it with your real data.

Open Regex Tester โ†’

Recommended Tools & Resources

Level up your workflow with these developer tools:

Try Neon Postgres → Try DigitalOcean → Mastering Regular Expressions →

Dev Tools Digest

Get weekly developer tools, tips, and tutorials. Join our developer newsletter.

Frequently Asked Questions

What are the most useful regex patterns?

The top 5 most-used regex patterns: email validation, URL matching, phone number validation, IP address matching, and date format validation. Our cheat sheet includes 20 copy-paste patterns with live testing links.

How do I learn regex quickly?

Start with 3 concepts: character classes (\d, \w, \s), quantifiers (*, +, ?, {n}), and anchors (^, $). Practice with our online regex tester at devtoolkit.cloud/tools/regex-tester. Most regex can be built from these basics.

Is regex the same in all programming languages?

Mostly, but not exactly. JavaScript, Python, Java, and most languages support PCRE (Perl-Compatible Regular Expressions). Differences exist in lookahead/lookbehind support, Unicode handling, and flags. Always test in your target language.