RegexCraft

ISO 8601 date regex (YYYY-MM-DD)

Matches calendar dates in the ISO 8601 YYYY-MM-DD form and captures them as named groups year, month and day. Months are limited to 01 to 12 and days to 01 to 31.

Open in the tester

The pattern

/(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])/g

Sample text, 3 matches

Releases: v1 shipped on 2024-03-14, v2 on 2025-11-02, and the patch on 2026-01-25. 2026-13-01 is not a date.

Point at a coloured group to see it in the pattern, the text and the explanation.

What it deliberately doesn’t do

  • It does not know month lengths: 2025-02-31 matches. Check the date with a date library.
  • Times and time zones (2025-01-25T10:00Z) are not part of the match.

Explained, in order

  1. (?<year>\d{4})Group 1.Named capturing group 'year' (#1) — captured text is accessible by name
  2. \dMatches a digit (0–9)
  3. {4}The preceding element matches exactly 4 times (greedy)
  4. -Matches the character '-' literally
  5. (?<month>0[1-9]|1[0-2])Group 2.Named capturing group 'month' (#2) — captured text is accessible by name
  6. 0[1-9]|1[0-2]Alternation — matches any ONE of the 2 alternatives listed above/below
  7. 0Matches the character '0' literally
  8. [1-9]Character class — matches one character from: '1' through '9'
  9. 1Matches the character '1' literally
  10. [0-2]Character class — matches one character from: '0' through '2'
  11. -Matches the character '-' literally
  12. (?<day>0[1-9]|[12]\d|3[01])Group 3.Named capturing group 'day' (#3) — captured text is accessible by name
  13. 0[1-9]|[12]\d|3[01]Alternation — matches any ONE of the 3 alternatives listed above/below
  14. 0Matches the character '0' literally
  15. [1-9]Character class — matches one character from: '1' through '9'
  16. [12]Character class — matches one character from: '1', '2'
  17. \dMatches a digit (0–9)
  18. 3Matches the character '3' literally
  19. [01]Character class — matches one character from: '0', '1'

In your language

JavaScript

The engine this page was tested with.

const re = /(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])/g;

// Example
const matches = [...str.matchAll(/(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])/g)];
for (const m of matches) {
  console.log(m[0], m.index, m.groups);
}

Python

Translated. Named groups are rewritten to (?P<name>…).

import re

# Note: named groups rewritten to Python's (?P<name>...) syntax.

pattern = re.compile(r"(?P<year>\d{4})-(?P<month>0[1-9]|1[0-2])-(?P<day>0[1-9]|[12]\d|3[01])")

# Example
for m in pattern.finditer(text):
    print(m.group(0), m.start(), m.groups())

Go

Translated. Go's RE2 has no lookaround or backreferences.

package main

import (
	"fmt"
	"regexp"
)

var re = regexp.MustCompile(`(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])`)

func main() {
	for _, match := range re.FindAllStringSubmatch(text, -1) {
		fmt.Println(match[0], match)
	}
}

Rust

Translated. The regex crate has no lookaround or backreferences.

use regex::Regex;

// Note: Rust finds all matches via find_iter / captures_iter (no 'g' flag).

fn main() {
    let re = Regex::new(r#"(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])"#).unwrap();

    for m in re.find_iter(text) {
        println!("{} at {}", m.as_str(), m.start());
    }
}