FormatterHQ

Regex Patterns Library

Search 45 ready-made regex patterns for emails, phone numbers, URLs, dates, numbers and IDs. Each is tested against real examples and can be copied as JavaScript, Python, PHP or Java code.

Loading tool…
Ready.

How to use the Regex Patterns Library

  1. Type what you want to match in the search box, such as email, uuid or zip, or pick a category.
  2. Read the one-line explanation and the badge: some patterns validate a whole value, others find matches inside a longer text.
  3. Open Test it · examples · flavour notes and type your own values, one per line, to see which ones match.
  4. Copy the raw pattern, or ready-to-paste code for JavaScript, Python, PHP or Java.

Ready-to-use regex patterns, with examples

Most regular expressions you need have been written many times before: an email check in a sign-up form, a postal code field, a UUID in an API route. This library collects 45 regex patterns for those everyday jobs. Every pattern lists values it matches, values it rejects and the cases it deliberately does not check, and all of those examples are covered by automated tests in JavaScript, with the generated PHP, Python and Java code tested against the same examples.

Email regex: which one should you use?

Email is the pattern people look for most, and there is no single right answer. The library offers three, from loose to strict:

  • Simple: ^[^\s@]+@[^\s@]+\.[^\s@]+$ accepts anything shaped like name@domain.tld. Use it for a friendly first check while someone types.
  • HTML input type="email": the rule browsers apply to email form fields. It accepts user@localhost because intranet addresses need no dot, and it does not object to a..b@example.com. Use it when your server should agree exactly with the browser.
  • Practical RFC 5322 subset: a local part without leading, trailing or double dots, a domain with at least one dot, a letters-only top-level domain, and two lookaheads that enforce the 64-character local part and 254-character total limits.

Why full RFC validation by regex is impractical

The address grammar in RFC 5322 allows quoted local parts with spaces ("john doe"@example.com), comments in brackets, IP-address domains such as user@[192.0.2.1] and obsolete forms kept for old mail systems. A regex that covers all of it runs to thousands of characters, is hard to review, and still cannot tell whether the mailbox exists. Use a sensible pattern to catch typos, then confirm the address by sending a verification email. To check a whole list at once, the email validator explains why each address fails.

What is in the library

CategoryPatterns
Phone numbersE.164, US and Canada (NANP), international loose, Indian mobile
Postal codesUS ZIP and ZIP+4, UK postcode, Canadian postal code, Indian PIN
Dates and timesYYYY-MM-DD, ISO 8601 date-time, DD/MM/YYYY, 24-hour time
Numbersinteger, decimal, positive number, thousands separators, US dollar amounts
IdentifiersUUID, UUID v4, slug, username, hex colour, MAC address, semantic version, ISBN-10, ISBN-13, card number format
WebURL, URLs inside text, IPv4, IPv6, hostname, domain, HTML tag
Text and passwordsalphanumeric, letters only, names in any alphabet, no special characters, trimming, repeated words, password rules

A regex can only check the shape of a phone number. To check that a number is possible for its country and format it, use the phone number validator.

Whole values or matches inside text

Patterns marked “validates a whole value” start with ^ and end with $, so the entire input must match: ^\d{5}$ accepts 90210 but not zip 90210. Patterns marked “finds text inside a string” have no anchors and use the g flag, like the URL finder that pulls every link out of a paragraph and leaves the full stop at the end of a sentence outside the match.

Copying a pattern into your language

Patterns are written in JavaScript syntax, and the copy buttons convert them:

  • JavaScript: a regex literal with slashes escaped, e.g. /^\d{5}(?:-\d{4})?$/.
  • Python: re.compile(r'…') with flags such as re.IGNORECASE. Patterns that use \p{…} need the third-party regex module, and the copied code says so.
  • PHP: a single-quoted string with / delimiters for preg_match(), including the i or u modifier when the pattern needs it.
  • Java: Pattern.compile("…") with every backslash doubled and flags such as Pattern.CASE_INSENSITIVE.

One difference catches many people: in Python, PHP and Java, $ also matches just before a final line break, so "90210\n" passes ^\d{5}$. Use re.fullmatch() in Python or \z in PHP and Java, or trim the input first. Go's regexp package has no lookarounds or back-references, and the flavour notes say which patterns need changes there. The regex cheat sheet compares the main flavours feature by feature.

Know what a pattern does not check

A regex checks form, not meaning. The ISO date pattern accepts 2023-02-31, the card number pattern does not run the Luhn checksum, and the ISBN patterns do not verify the check digit. Each pattern lists those gaps under “What it does not validate”, with examples that still match, so you know which check to add in code. To take a pattern further, for example with a replacement or capture groups, paste it into the regex tester. The test boxes on this page run in your browser.

Frequently asked questions

What is the best regex for email validation?
For most forms, the practical RFC 5322 subset in this library is a good balance: it rejects common typos such as double dots or a missing top-level domain and enforces the length limits. If you want exactly the rule the browser uses for type="email" fields, use the HTML pattern instead. Either way, confirm the address by sending an email.
Why does a regex work in JavaScript but not in Python?
The usual causes are syntax and defaults. Python writes named groups as (?P…), its built-in re module has no \p{…} Unicode properties, and its $ also matches before a final line break. The Python copy button converts named groups and adds the right flags for you.
How do I allow only numbers with a regex?
Use ^\d+$ for whole numbers or ^[-+]?\d+$ to allow a sign. For decimals use the decimal pattern, ^[-+]?(?:\d+(?:\.\d+)?|\.\d+)$. In Python 3 and .NET \d also matches digits from other scripts, so write [0-9] if you need ASCII digits only.
What regex matches alphanumeric characters only?
^[a-zA-Z0-9]+$ allows ASCII letters and digits and nothing else. Do not use \w for this, because it also allows the underscore. For letters and digits in any alphabet, use ^[\p{L}\p{N}]+$ with the u flag in JavaScript.
Can a regex tell me whether a date or card number is real?
No. A regex checks the format only: 2023-02-31 has a valid date format and 4111 1111 1111 1112 has a valid card number format. Parse dates with your language’s date functions and check card numbers with the Luhn algorithm or your payment provider.

Last updated October 17, 2026

Related tools

Random Data from Regex Generate random strings that match a regular expression — ideal for test data such as IDs, codes and formats. Regex Cheat Sheet Interactive regex cheat sheet: anchors, classes, quantifiers, groups, lookarounds, flags and Unicode, with clickable examples and a flavour table. Regex Escape Escape special characters so any text can be matched literally inside a regular expression in JavaScript, PCRE, Python or Java. Regex Explainer Break a regular expression into tokens and explain each part in plain English: groups, quantifiers, classes, anchors and lookarounds. Regex Tester Test JavaScript regular expressions live: highlight matches, inspect capture groups and named groups, try flags and preview replacements. SemVer Checker & Range Tester Validate, compare and sort semantic versions and test them against npm-style ranges like ^1.2.0, ~2.3 or >=1.0 <2. Snowflake ID Decoder Convert Twitter/X, Discord, Instagram and other Snowflake IDs to timestamps and show worker, process and sequence bits. Time Sheet Calculator Add up weekly work hours from start and end times with breaks. Calculates overtime per day or week and optional gross pay. TOTP / 2FA Code Generator Generate live TOTP 2FA codes from a Base32 secret or otpauth:// URI, create new secrets and QR codes for authenticator apps. ULID Decoder Decode ULIDs to their embedded timestamp and random part, and convert between ULID and UUID formats. Un-Google Link Remove Google redirect wrappers (google.com/url, AMP, image results) and tracking parameters to get the real destination URL. Unix Timestamp Converter Convert Unix epoch timestamps to human dates and dates to timestamps, in UTC, local time and any time zone.