Regex examples
18 regular expressions for things developers check every day, each explained token by token, with examples it matches and rejects, its limits, and code for JavaScript, Python, PHP and C#.
Patterns at a glance
| For | Pattern |
|---|---|
| US ZIP code | /^[0-9]{5}(?:-[0-9]{4})?$/ |
| Phone number | /^\+[1-9][0-9]{1,14}$/ |
| URL | /^https?:\/\/[^\s\/?#]+(?:[\/?#]\S*)?$/i |
| Password rules | /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9]).{8,}$/ |
| Date (YYYY-MM-DD) | /^[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])$/ |
| Time (HH:MM) | /^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/ |
| Digits only | /^[0-9]+$/ |
| Hex colour | /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i |
| Credit card number | /^(?:[0-9][ -]?){11,18}[0-9]$/ |
| URL slug | /^[a-z0-9]+(?:-[a-z0-9]+)*$/ |
Longer patterns, such as the email, IPv6 and UK postcode patterns, are on their own pages. Paste any of them into the regex tester, or open a page: each one has the tester built in, loaded with the pattern and its examples.
Every pattern, explained
Forms and contact details
- Email addressThe HTML standard's pattern for type="email", and why RFC 5322 is the wrong target
- UK postcodeRoyal Mail's six formats plus GIR 0AA, compared with the government's own pattern
- US ZIP codeFive digits with an optional -1234 extension, stored as text
- Phone numberInternational E.164 format, plus UK landlines and mobiles
- URLhttp and https links, finding URLs in text, and when to use the URL parser instead
- Password rulesThe classic lookahead rules, and what NIST now recommends instead
Networks
- IPv4 addressDotted decimal with each part from 0 to 255, and the leading-zero trap
- IPv6 addressFull and :: compressed hexadecimal forms, checked against a real parser
Numbers, dates and IDs
- Date (YYYY-MM-DD)ISO 8601 calendar dates, and the extra step for 31 February
- Time (HH:MM)24-hour HH:MM, optional seconds and 12-hour AM/PM
- Digits onlyOnly 0 to 9, why not \d, and patterns for integers and decimals
- UUIDThe 8-4-4-4-12 form with RFC 9562 version and variant digits
- Hex colour#rgb, #rgba, #rrggbb and #rrggbbaa, as CSS accepts them
- Credit card numberThe shape of a card number, brand prefixes, and why Luhn needs code
Text and markup
- Trim whitespaceBoth ends, every line or inner runs, and what \s means in each language
- URL slugLowercase words joined by single hyphens, and how to make one from a title
- HTML tagsFinding tags in simple markup, where it breaks, and the parsers to use instead
- Matching across linesThe s flag, [\s\S], the m flag, and Windows line endings
How these patterns are tested
Every pattern on these pages is a real regular expression in the site's source, and every "matches" and "rejected" example is run against it when the site is built: if an example is wrong, the build stops. The table of tokens on each page must join back into the exact pattern, so the explanation cannot drift from the code. The JavaScript, Python and C# examples are run for real and their output compared with the comments; PHP, which we cannot run on our build machine, is checked against its documentation, and the build confirms that each PHP string literal hands PHP exactly the pattern that was tested.
Facts that do not come from running code, such as the format of a UK postcode or what NIST says about passwords, link to the official source.
The same pattern in four languages
The patterns use syntax that means the same in JavaScript, Python, PHP and .NET. That is why they write [0-9] rather than \d, and why each page's code makes one adjustment per language for anchoring. The differences that most often cause a pattern to behave differently after it is copied:
| JavaScript | Python | PHP | C# (.NET) | |
|---|---|---|---|---|
| \d (digit) | only 0-9 | any Unicode digit (re.ASCII for 0-9) | only 0-9 (without the u modifier) | any Unicode digit (ECMAScript for 0-9) |
| $ without m | only at the very end | also before a final \n | also before a final \n (unless D) | also before a final \n |
| Whole-string match | ^…$ | re.fullmatch() | ^…$ with D | ^…\z |
| Named group | (?<name>…) | (?P<name>…) only | (?<name>…) or (?P<name>…) | (?<name>…) |
The $ difference is the one that lets bad data in: in Python, PHP and .NET, ^[0-9]+$ accepts "123\n". See digits only for the \d difference and matching across lines for the flags. For the full syntax of each engine, see the MDN regular expressions guide, Python's re module, PHP's PCRE functions and .NET's regular expression language reference.
A regex checks shape, not truth
A pattern can say that a string looks like an email address, a postcode or a card number. It cannot say that the mailbox exists, the postcode has been issued or the card has money on it. Use a regex to catch typing mistakes early and give a helpful message, then confirm the value the proper way: a confirmation email, an address lookup, a payment provider. For structured formats such as URLs, IP addresses, dates and HTML, the parser in your language knows the rules better than any pattern, and each page shows it.
Finally, keep patterns simple enough to read. Nested quantifiers such as (a+)+ can take exponentially long on text that almost matches, which is called catastrophic backtracking. The regex tester stops such a pattern after a quarter of a second; a server would simply hang.
Questions
Are these regex patterns safe to copy into production code?
Each one is tested against the examples on its page in JavaScript, Python and C#. Read the limits section before you rely on one: most check the format only, and some deliberately reject rare but valid forms to stay readable.
Why do these patterns use [0-9] instead of \d?
Because in Python and .NET, \d also matches digits from other scripts, such as Arabic-Indic digits. [0-9] means only the ASCII digits in every language.
Which regex flavour is the regex tester?
JavaScript, the engine built into your browser. The patterns on these pages are written to behave the same way in Python, PHP and .NET, and each page shows the code for all four.