Regex Pattern Library
Copy-paste regular expressions for the most common validation and extraction tasks. Each pattern includes examples, variations, and a plain-English explanation.
Open Regex TesterIdentity & Contact
Email Address
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ This regex matches standard email addresses as defined by most practical implementations. It checks for a local part containing alphanumeric characters, dots, underscores, percent signs, plus signs, and hyphens, followed by an @ symbol, a domain name, and a top-level domain of at least two characters. While it does not cover every edge case in RFC 5322, it handles the vast majority of real-world email addresses correctly.
URL
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*) This regex matches HTTP and HTTPS URLs including optional www prefix, domain name, and optional path, query string, and fragment components. It supports standard URL characters and percent-encoded values. The pattern works well for extracting URLs from plain text and for basic URL validation in forms.
US Phone Number
^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ This regex matches US phone numbers in a variety of common formats including with or without country code, parentheses around the area code, and dashes, dots, or spaces as separators. It handles formats like (555) 123-4567, 555-123-4567, 5551234567, and +1 555 123 4567. The pattern is flexible enough for user input while still enforcing the 10-digit US format.
International Phone Number
^\+?[1-9]\d{1,14}$ This regex matches international phone numbers following the E.164 standard. It allows an optional leading plus sign followed by a country code and subscriber number totaling up to 15 digits. The E.164 format is the most universally accepted phone number format and is used by services like Twilio and most telephony APIs.
Username
^[a-zA-Z0-9_-]{3,20}$ This regex matches usernames between 3 and 20 characters containing only letters, numbers, underscores, and hyphens. This is one of the most common username format policies used across web applications. It prevents special characters that could cause issues in URLs, file paths, or SQL queries while still allowing readable, flexible usernames.
Network
IPv4 Address
^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$ This regex matches valid IPv4 addresses by validating each of the four octets is between 0 and 255. It correctly rejects values like 256.1.1.1 or 999.999.999.999 that simpler patterns would match. Each octet allows optional leading zeros and single-digit values. The pattern is commonly used in network configuration validation and log file parsing.
IPv6 Address
^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$ This regex matches standard IPv6 addresses in their full notation with eight groups of four hexadecimal digits separated by colons. IPv6 is the successor to IPv4, providing a vastly larger address space. This pattern matches the expanded form of IPv6 addresses, which is the most explicit representation used in configuration files and documentation.
Domain Name
^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$ This regex matches valid domain names with one or more subdomains and a top-level domain (TLD) of at least two characters. Each label (part between dots) can be up to 63 characters, must start and end with an alphanumeric character, and can contain hyphens in the middle. This follows the DNS naming rules defined in RFC 1035.
Date & Time
Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ This regex matches dates in the ISO 8601 format YYYY-MM-DD. It validates that months are between 01-12 and days are between 01-31. The ISO 8601 date format is the international standard and is the recommended format for data interchange, APIs, and database storage. Note that this pattern does not validate logical date correctness (e.g., February 30).
Date (MM/DD/YYYY)
^(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01])/\d{4}$ This regex matches dates in the US-style MM/DD/YYYY format with forward slash separators. It validates that months are 01-12 and days are 01-31. This format is commonly used in US-facing applications, government forms, and legacy systems. For international or API use, prefer the ISO 8601 YYYY-MM-DD format instead.
24-Hour Time
^([01]\d|2[0-3]):([0-5]\d)$ This regex matches times in 24-hour format (HH:MM) where hours range from 00 to 23 and minutes range from 00 to 59. The 24-hour clock format is the international standard and is unambiguous unlike the 12-hour AM/PM format. It is commonly used in scheduling systems, timetables, military applications, and most programming contexts.
ISO 8601 DateTime
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T([01]\d|2[0-3]):([0-5]\d):([0-5]\d)(\.\d+)?(Z|[+-]([01]\d|2[0-3]):[0-5]\d)?$ This regex matches ISO 8601 datetime strings including date, time, optional fractional seconds, and optional timezone offset. ISO 8601 is the standard format used by JSON, REST APIs, databases, and most programming languages. The pattern validates the full datetime structure including the T separator and Z or +/-HH:MM timezone designator.
Financial & Postal
Credit Card Number
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\d{3})\d{11})$ This regex matches major credit card number formats including Visa, Mastercard, American Express, Diners Club, Discover, and JCB. Each card type has a distinct prefix and length, and this pattern validates the correct prefix-length combinations. Note that this only checks the format, not the Luhn checksum or whether the card is actually valid or active.
US Social Security Number
^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$ This regex matches US Social Security Numbers in the standard XXX-XX-XXXX format with dashes. It includes negative lookaheads to exclude invalid SSNs: area numbers 000, 666, and 900-999 are never assigned, group numbers 00 and serial numbers 0000 are also invalid. This pattern follows the SSA's actual assignment rules for realistic validation.
US ZIP Code
^\d{5}(-\d{4})?$ This regex matches US ZIP codes in both the standard 5-digit format and the extended ZIP+4 format with an optional dash and four additional digits. ZIP codes are used by the US Postal Service for mail delivery routing. The 5-digit code identifies a delivery area, and the optional +4 extension narrows it to a specific delivery segment.
UK Postal Code
^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$ This regex matches UK postcodes, which follow several alphanumeric formats such as A1 1AA, A11 1AA, AA1 1AA, AA11 1AA, A1A 1AA, and AA1A 1AA. The outward code (before the space) identifies the postal district, and the inward code (after the space) identifies the specific delivery point. The space between the two parts is optional in this pattern.
Canadian Postal Code
^[A-CEGHJ-NPRSTVXY]\d[A-CEGHJ-NPRSTV-Z]\s?\d[A-CEGHJ-NPRSTV-Z]\d$ This regex matches Canadian postal codes in the A1A 1A1 alternating letter-digit format. It excludes letters D, F, I, O, Q, and U, which are not used in Canadian postal codes to avoid confusion with similar-looking digits. The first letter indicates the province or territory. The optional space between the two three-character groups follows common formatting conventions.
Colors & Markup
Hex Color Code
^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$ This regex matches hexadecimal color codes in 3-digit, 6-digit, and 8-digit formats with an optional # prefix. The 3-digit shorthand (#FFF) expands to 6 digits (#FFFFFF), and the 8-digit format includes an alpha channel for transparency. Hex colors are the most common way to specify colors in CSS, HTML, and design tools.
RGB Color
^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$ This regex matches CSS RGB color function syntax in the traditional comma-separated format. Each color channel value is 1-3 digits (0-255 range is not enforced by the regex). The pattern allows optional whitespace around the commas for flexible formatting. RGB is one of the most widely used color models in web development and digital design.
HTML Tag
<\/?([a-zA-Z][a-zA-Z0-9]*)\b[^>]*> This regex matches both opening and closing HTML tags, including self-closing tags and tags with attributes. It captures the tag name and handles attributes with various quoting styles. While useful for simple HTML extraction and analysis, regex should not be used for full HTML parsing due to the complexity and nesting of real-world HTML documents.
Markdown Link
\[([^\]]+)\]\(([^)]+)\) This regex matches Markdown-style links in the format [text](url). It captures both the link text and the URL in separate groups. Markdown links are used in README files, documentation, static site generators, and many content management systems. The pattern works for inline links but does not match reference-style links.
Markdown Heading
^(#{1,6})\s+(.+)$ This regex matches Markdown ATX-style headings from H1 (one #) through H6 (six #). It captures the heading level indicators and the heading text separately. The multiline flag allows it to match headings on any line in a document. Markdown headings are used in documentation, README files, and content management systems for document structure.
Validation & Formatting
Strong Password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$ This regex enforces a strong password policy requiring at least 8 characters with at least one lowercase letter, one uppercase letter, one digit, and one special character. It uses positive lookaheads to check each requirement independently without enforcing a specific character order. This is a common password strength pattern used in registration forms and account security settings.
URL Slug
^[a-z0-9]+(?:-[a-z0-9]+)*$ This regex matches URL slugs — the URL-friendly version of a title or name. Slugs contain only lowercase letters, numbers, and hyphens, with no consecutive hyphens or leading/trailing hyphens. They are used in blog post URLs, product pages, and any resource where a readable, SEO-friendly URL is needed.
File Extension
\.(\w+)$ This regex matches file extensions at the end of a string by capturing everything after the last dot. It uses the word character class (letters, digits, underscore) which covers virtually all standard file extensions. The pattern is commonly used in file upload validation, content type detection, and file system operations.