Data & Text
| Use | Pattern | What it matches | Action |
|---|---|---|---|
| Digits only | ^\d+$ |
One or more digits, with nothing else before or after. Use this for a whole-number string, not decimals or negative numbers. | Test |
| Username | ^[A-Za-z0-9_]{3,16}$ |
Three to sixteen ASCII letters, digits, or underscores. Adjust the length range and allowed characters to match your rules. | Test |
| Email-shaped text | ^[\w.+-]+@[\w-]+(?:\.[\w-]+)+$ |
A practical check for a typical email shape. Email address rules are more complex, so use confirmation email rather than this pattern as proof an address is valid. | Test |
| HTTP or HTTPS URL | ^https?:\/\/[\w.-]+(?::\d+)?(?:[/?#][^\s]*)?$ |
A basic web URL with an optional port and path, query, or fragment. It is intentionally not a full URL parser and does not cover every valid hostname. | Test |
| Date (YYYY-MM-DD) | ^\d{4}-\d{2}-\d{2}$ |
Checks the four-digit year, two-digit month, and two-digit day shape only; it will also match impossible dates such as 2026-19-42. | Test |
| UUID | ^[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}$ |
Matches the standard 8-4-4-4-12 hexadecimal UUID layout. It checks the shape, not a particular UUID version. | Test |
| Hex colour | ^#?(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$ |
Matches a 3- or 6-digit hexadecimal colour, with an optional leading #. | Test |
| URL slug | ^[a-z0-9]+(?:-[a-z0-9]+)*$ |
Lowercase letters and digits separated by single hyphens, with no leading, trailing, or repeated hyphens. | Test |
| Repeated whitespace | \s{2,} |
Finds runs of two or more whitespace characters, including spaces, tabs, and line breaks. | Test |
| Hashtag | #[A-Za-z0-9_]+ |
Finds a # followed by one or more ASCII letters, digits, or underscores. | Test |
| UK postcode | ^[A-Za-z]{1,2}\d[A-Za-z\d]?\s?\d[A-Za-z]{2}$ |
The general shape of a UK postcode, with an optional space before the inward code (e.g. SW1A 1AA, M1 1AE). It does not check that the postcode actually exists. | Test |
| IPv4 address | ^(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)$ |
Four dot-separated numbers, each limited to 0-255. Unlike a loose \d{1,3} check, this rejects values such as 999.1.1.1. | Test |
| Semantic version | ^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$ |
A MAJOR.MINOR.PATCH version with optional pre-release and build metadata, such as 1.4.0, 2.0.0-beta.1 or 1.0.0+build5. | Test |
| Duplicate words | \b(\w+)\s+\1\b |
Finds a word immediately repeated ("the the"). The \1 is a backreference to whatever the first group captured. Use the i flag to ignore case. | Test |
| Leading and trailing whitespace | ^\s+|\s+$ |
Matches whitespace at the very start or end of a string. Use it with the g flag in a replace to trim input. | Test |
| Strong password shape | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{12,}$ |
At least 12 characters including a lowercase letter, an uppercase letter and a digit, using lookaheads. Length matters more than character mix, so treat this as a minimum rather than a strength meter. | Test |
| HTML tag | <\/?[A-Za-z][A-Za-z0-9]*(?:\s[^>]*)?> |
Finds opening and closing HTML tags in text. Fine for quick extraction or stripping, but regex cannot reliably parse nested HTML, so use a real parser for anything important. | Test |
| Quoted string | "(?:[^"\\]|\\.)*" |
A double-quoted string that allows backslash-escaped characters inside, so an escaped quote does not end the match early. | Test |
Reading a pattern
Most of the samples above are built from a handful of pieces. Once you recognise these, you can read (and adjust) almost any pattern:
| Token | Meaning |
|---|---|
^ and $ | Start and end of the string. Without them a pattern matches anywhere inside the text, so \d+ will happily match the "42" in "abc42xyz". |
\d \w \s | A digit, a word character (letter, digit or underscore), and whitespace. The capitalised forms (\D, \W, \S) mean the opposite. |
[abc] [^abc] | Any one character from the set, or any one character not in the set. Ranges work too: [a-z0-9]. |
+ * ? | One or more, zero or more, and zero or one of the previous item. |
{3,16} | A specific repeat count or range for the previous item. |
(...) (?:...) | A capturing group, and a non-capturing group. Use the second form when you only need grouping, not a captured value. |
(?=...) | A lookahead: a condition that must be true at this position without consuming any text. Stacking several, as in the password sample, checks multiple rules at once. |
| | Alternation: match the left side or the right side. |
Flags
Flags change how the whole pattern behaves. The ones you'll use most are g (find every match, not just the first), i (ignore case), m (make ^ and $ match at each line, not just the whole string) and s (let . match line breaks). The samples above are written without flags, so add them in the tester as needed.
Common pitfalls
- Forgetting the anchors. A validation pattern without
^and$accepts any input that merely contains a valid value. - Unescaped special characters. A literal dot needs
\., otherwise.matches any character, soexample.comalso matches "exampleXcom". - Greedy matching.
<.+>grabs everything from the first<to the last>. Add?(<.+?>) to make it stop at the first closing one. - Catastrophic backtracking. Nested repeats such as
(a+)+$can take exponential time on a near-miss input. Avoid them in patterns that run on user-supplied text. - Assuming ASCII.
\wand[A-Za-z]don't match accented letters or non-Latin scripts. Use theuflag with\p{L}when names or international text are involved.
JavaScript vs other regex flavours
These patterns use JavaScript syntax, which is close to PCRE (used by PHP's preg_* functions) but not identical. Most of the samples work unchanged in PHP once wrapped in delimiters, e.g. preg_match('/^\d+$/', $value). Differences to watch for: PHP needs delimiters and flags placed after the closing one, JavaScript lacks some PCRE features such as possessive quantifiers, and named groups are written (?<name>...) in JavaScript but also accept (?P<name>...) in PCRE.
Patterns are examples, not complete validation rules. For dates, email addresses, URLs, and other structured values, use a dedicated parser or server-side validation when correctness matters.
Open the Regex Tester to try a pattern against your own text.