Regular expression
/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/Pattern breakdown
| Part | Meaning |
|---|---|
Anchor or context | Use anchors when you need whole-string validation. |
Main token | The core token sequence describes the accepted text shape. |
Character class | Character classes limit which characters are valid. |
Quantifier | Quantifiers control how many characters or groups are accepted. |
Flags | Use language-specific flags such as i, m, or u only when needed. |
Should match
2026-07-162025-07-16
Should not match
16-07-2026not matching sample2026-07-16 invalid16-07-20262026-07-16
Test cases
| Input | Expected | Why it matters |
|---|---|---|
2026-07-16 | Match | Representative valid input for this pattern. |
2025-07-16 | Match | Representative valid input for this pattern. |
16-07-2026 | No match | Common invalid or boundary input. |
(empty string) | No match | Common invalid or boundary input. |
not matching sample | No match | Common invalid or boundary input. |
2026-07-16 invalid | No match | Common invalid or boundary input. |
16-07-20262026-07-16 | No match | Common invalid or boundary input. |
JavaScript
const re = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
re.test(input);Python
import re
bool(re.search(r"^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$", text))PHP
$ok = preg_match('/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/', $value) === 1;Java
Pattern pattern = Pattern.compile("^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$");
pattern.matcher(value).find();Go
re := regexp.MustCompile(`^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$`)
ok := re.MatchString(value)Notes and production use
ISO Date YYYY-MM-DD regex is useful as a practical starting point. Test it against your real input, avoid using it as the only security control, and prefer a parser when the format has complex grammar.
Performance tip: avoid running complex regular expressions repeatedly on very large untrusted strings without limits. Prefer anchored validation patterns, cap input length before matching, and use a parser when the target format has nested grammar.