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?▾
Should I use regex to parse HTML?▾
How do I make regex patterns case-insensitive?▾
What is a lookahead in regex?▾
Can I test these patterns before using them in my code?▾
Try it now — free, private, and instant
Paste any regex pattern, toggle flags, and see matches highlighted in real time.
Launch the Regex Tester