# Regex for a YYYY-MM-DD date

`/^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/`

**Match a whole string consisting of (exactly 4 of a digit) captured as "year", then "-", then (exactly 2 of a digit) captured as "month", then "-", then (exactly 2 of a digit) captured as "day".**

## Step by step

- `^` — Anchor to the start of the input.
- `(?<year>\d{4})` — Match (exactly 4 of a digit) captured as "year".
- `-` — Match "-".
- `(?<month>\d{2})` — Match (exactly 2 of a digit) captured as "month".
- `-` — Match "-".
- `(?<day>\d{2})` — Match (exactly 2 of a digit) captured as "day".
- `$` — Anchor to the end of the input.

## Warnings

### Anchors match string ends, not line ends (info)

Without the m flag, ^ and $ match only the very start and end of the input — not the start and end of each line. On multi-line input this is often not what was intended.

**Fix:** Add the m flag if the pattern should apply per line.

## Details

- Capture groups: 3
- Named groups: year, month, day

---

Canonical URL: https://regex-explainer.gumballtools.com/regex/regex-for-date-yyyy-mm-dd
JSON API: `GET https://regex-explainer.gumballtools.com/api/v1/explain?pattern=...`
MCP endpoint: `https://regex-explainer.gumballtools.com/api/mcp`
