20 Common Regex Patterns Every Developer Should Know

Published on · 797 words

Want to follow along with this guide? Open the free Regex Tester and test every pattern in real time with match highlighting and explanations.

Regular expressions are everywhere in software development, from validating user input to parsing configuration files and extracting data from unstructured text. While every project has unique needs, there is a core set of regex patterns that developers reach for again and again. Having these patterns in your toolkit saves time and reduces bugs.

This guide presents 20 of the most commonly used regex patterns, each with a clear explanation of how it works and when to use it. You can copy any pattern directly into your code or test it first in our free Regex Tester to see how it behaves with your own data. Whether you are building a signup form, parsing server logs, or cleaning user-submitted content, these patterns will cover most of your everyday needs.

Validation Patterns: Email, URL, and Phone Numbers

Email validation is one of the most common uses of regex. A practical pattern like [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} covers the vast majority of valid email addresses. While the full RFC 5322 specification allows far more complex addresses, this simpler pattern is sufficient for most web forms and avoids rejecting valid addresses with unusual but legal characters.

URL matching requires handling multiple protocols, domain formats, paths, query strings, and fragments. The pattern https?:\/\/[\w\-._~:/?#\[\]@!$&'()*+,;=%]+ matches both HTTP and HTTPS URLs and captures the full address including path and query parameters. For stricter validation, you can tighten the character class or add domain-specific rules.

Phone number patterns vary by country. A flexible US pattern like \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} handles formats like (555) 123-4567, 555-123-4567, and 5551234567. For international numbers, consider a pattern like \+?[\d\s\-()]{7,15} that accepts optional country codes and various separators.

  • Email: [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
  • URL: https?:\/\/[\w\-._~:/?#\[\]@!$&'()*+,;=%]+
  • US Phone: \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
  • International Phone: \+?[\d\s\-()]{7,15}
  • Credit Card: \b(?:\d{4}[- ]?){3}\d{4}\b

Network and Data Patterns: IP Addresses, Dates, and Hex Colors

IPv4 address validation requires matching four octets, each between 0 and 255. The pattern \b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b uses alternation to enforce the valid range for each octet. While lengthy, this pattern correctly rejects invalid addresses like 999.999.999.999.

Date patterns depend on the expected format. For ISO dates (YYYY-MM-DD), use \d{4}[-/](?:0[1-9]|1[0-2])[-/](?:0[1-9]|[12]\d|3[01]). This validates month ranges (01-12) and day ranges (01-31). Note that it does not check for impossible dates like February 31 — that requires application-level logic.

Hex color codes follow a simple structure: # followed by 3 or 6 hexadecimal digits. The pattern #(?:[0-9a-fA-F]{3}){1,2}\b matches both shorthand (#F0A) and full (#FF00AA) forms. This is useful for validating CSS color values in user-submitted styles or design tools.

Text Processing Patterns: HTML Tags, Passwords, and More

Matching HTML tags with regex requires care. A pattern like <([a-z][a-z0-9]*)\b[^>]*>.*?<\/\1> matches paired opening and closing tags using a backreference (\1) to ensure the closing tag matches the opening one. However, regex is not ideal for parsing complex or nested HTML — use a proper HTML parser for that task.

Strong password validation patterns enforce complexity requirements. The pattern ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$ uses lookaheads to require at least one lowercase letter, one uppercase letter, one digit, and one special character, with a minimum length of 8.

Other useful patterns include: US ZIP codes with \b\d{5}(?:-\d{4})? matching both 5-digit and ZIP+4 formats, and whitespace normalization with \s+ to find runs of spaces, tabs, or newlines that should be collapsed to a single space.

  • IPv4: \b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b
  • ISO Date: \d{4}[-/](?:0[1-9]|1[0-2])[-/](?:0[1-9]|[12]\d|3[01])
  • Hex Color: #(?:[0-9a-fA-F]{3}){1,2}\b
  • Strong Password: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
  • US ZIP: \b\d{5}(?:-\d{4})?\b

Frequently Asked Questions

Can regex fully validate an email address?
A regex can catch most invalid formats, but the full email specification (RFC 5322) is extremely complex. For practical purposes, a simpler regex combined with a verification email is the best approach. The patterns in this guide cover over 99% of real-world email addresses.
Should I use regex to parse HTML?
Regex can handle simple, well-structured HTML extraction, but it breaks down with nested tags, attributes containing >, and malformed markup. For robust HTML parsing, use a dedicated parser like DOMParser in the browser or a library like cheerio in Node.js.
How do I make regex patterns case-insensitive?
Use the i flag (case-insensitive flag). For example, /hello/i matches 'hello', 'Hello', 'HELLO', and any other case variation. You can also use character classes like [hH][eE][lL][lL][oO] for fine-grained control without the flag.
What is a lookahead in regex?
A lookahead is a zero-width assertion that checks whether a pattern follows (or does not follow) at the current position without consuming characters. Positive lookahead (?=...) requires the pattern to match; negative lookahead (?!...) requires it not to match. They are commonly used for password validation.
Can I test these patterns before using them in my code?
Yes. Our free Regex Tester lets you paste any pattern, set flags, and test it against sample text in real time. You can see matches highlighted, view captured groups, and read an explanation of each part of the pattern.

Try it now — free, private, and instant

Paste any regex pattern, toggle flags, and see matches highlighted in real time.

Launch the Regex Tester