Regex pattern library
Ready-made regular expressions for emails, URLs, dates, IP addresses, UUIDs and more.
20 patterns.
Web
- ^[^\s@]+@[^\s@]+\.[^\s@]{2,}$Email address
- A practical check: something, an at sign, a domain with a dot. Send a confirmation email to be sure.
- ^https?:\/\/[^\s/$.?#][^\s]*$URL with http or https
- A web address that starts with http:// or https://.
- ^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$Domain name
- Labels of letters, digits and hyphens, ending in a top-level domain.
- ^[a-z0-9]+(?:-[a-z0-9]+)*$URL slug
- Lower case words joined by single hyphens.
Network
- ^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$IPv4 address
- Four numbers from 0 to 255 with dots between them.
- ^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$MAC address
- Six pairs of hex digits, with colons or hyphens.
Identifiers
- ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$UUID
- Any standard UUID, versions 1 to 8.
- ^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$Hex colour
- A CSS colour with 3, 4, 6 or 8 hex digits.
- ^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$Semantic version
- Major, minor and patch, with an optional pre-release and build.
- ^[a-zA-Z0-9_]{3,20}$Username
- Three to twenty letters, digits or underscores.
Dates and times
- ^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$Date as YYYY-MM-DD
- An ISO 8601 date. It checks the shape, not whether the day exists in that month.
- ^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$Time as HH:MM
- A 24-hour time, with optional seconds.
- ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$ISO 8601 date and time
- A timestamp with a time zone, as APIs send it.
Numbers
- ^-?\d+$Whole number
- Digits, with an optional minus sign.
- ^-?(?:\d+\.?\d*|\.\d+)$Decimal number
- A number with an optional decimal part.
- ^\+[1-9]\d{6,14}$Phone number, international
- E.164 form: a plus sign, the country code and the number, no spaces.
Text
- ^\s+|\s+$Leading and trailing spaces
- Finds whitespace at either end. Replace with nothing to trim. Use the g flag.
- {2,}Repeated spaces
- Finds two or more spaces in a row. Replace with one space. Use the g flag.
- ^\s*$Blank line
- A line that is empty or holds only whitespace. Use the m flag.
- <\/?[a-zA-Z][^>]*>HTML tag
- Finds opening and closing tags. Fine for a quick clean-up, not for parsing real HTML.
Missing a feature or found a bug in Regex pattern library? Tell me on X (@_andrewtran) or by email.
How to use Regex pattern library
- Type what you want to match, such as email or date.
- Read what the pattern accepts.
- Copy the pattern into your code or the regex tester.
Questions and answers
Do these patterns work in every language?
They use syntax shared by JavaScript, Python, Java, Go and PCRE. In a string literal you may need to double the backslashes.
Is the email pattern complete?
No pattern is. It catches obvious mistakes, which is what a form needs. The only real test of an address is to send it a message.
Related tools
SQL formatter
Format SQL with clean indentation for PostgreSQL, MySQL, SQLite and SQL Server, or minify it to one line.HTML formatter
Beautify HTML with consistent indentation, or minify it by removing comments and spare whitespace.CSS formatter
Beautify CSS rules and media queries, or minify CSS with strings and comments handled safely.JSON viewer
Explore JSON as a collapsible tree, with counts for every object and list.