20+ Common Regular Expression Examples Every Developer Needs
If you spend enough time writing code, you will inevitably need to extract, replace, or validate text. While writing regex patterns from scratch is a great skill, sometimes you just need a reliable, copy-pasteable snippet to get the job done fast.
In this resource, we've compiled a bookmarkable list of the most common regular expression examples for developers. Whether you need to validate an IP address, parse a URL, or match HTML tags, these regex snippets have you covered.
Bookmark this page (Cmd/Ctrl + D) so you never have to search for these common regex patterns again!
Interactive Tester
Want to test these patterns against your own data? Use our interactive regex tester below, or visit the full Interactive Regex Tester and Cheat Sheet for a larger workspace.
1. Web & URLs
Extract Domain from URL
Extracts the domain name from a standard HTTP/HTTPS URL.
^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)
Validate URL
A robust regex to check if a string is a valid URL structure.
^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$
Match HTML Tags
Matches opening and closing HTML tags. (Note: use DOM parsers for complex HTML manipulation, but this is great for quick extraction).
<\/?[\w\s="/.':;#-\/\?]+>
Hex Color Codes
Matches standard 3-digit or 6-digit hex color codes.
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
2. Validation & Formatting
Email Validation
The standard, practical email validation pattern used by most developers (as detailed in our guide on Regex Form Validation in JavaScript).
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Strong Password
Requires at least 8 characters, 1 uppercase, 1 lowercase, 1 number, and 1 special character.
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
IPv4 Address
Accurately matches valid IPv4 addresses ranging from 0.0.0.0 to 255.255.255.255.
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
IPv6 Address
Basic validation for IPv6 addresses.
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
3. Dates & Numbers
Date (YYYY-MM-DD)
Validates standard ISO 8601 date formats.
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Time (HH:MM 24-Hour)
Matches 24-hour time formats.
^([01]\d|2[0-3]):?([0-5]\d)$
Positive or Negative Number (with optional decimals)
Matches whole numbers or decimals, positive or negative.
^-?\d+(\.\d+)?$
Credit Card Numbers (Basic)
Matches standard 16-digit credit cards, including optional hyphens or spaces.
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$
4. Text Processing & Data Extraction
Match Any Whitespace
Useful for cleaning up strings and normalizing spaces.
\s+
Match Phone Numbers (North America)
Matches common US/Canada phone number formats like (555) 555-5555, 555-555-5555, or 5555555555.
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
Extract Text Between Quotes
Extracts anything wrapped in double quotes. (If you want to understand how the underlying engine processes these lookahead/lookbehind structures, check out our guide on Regex Lookarounds).
"([^"\\]*(?:\\.[^"\\]*)*)"
Remove Trailing Slashes from URLs
Matches a trailing slash at the end of a string.
\/+$
Conclusion
Whether you are writing Python, JavaScript, Go, or Rust, these regex examples act as universal tools to accelerate your workflow.
If you are looking for an ad-free, powerful environment to test and save your own custom patterns, try RegexRef Pro and build your ultimate personal library!