Go (RE2)

ISO 8601 Date in GO

Match dates in ISO 8601 format: YYYY-MM-DD with valid month (01–12) and day (01–31) ranges.

Try it in the GO tester →

Pattern

regexGO
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])`)
	input := `2024-01-15`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

Year is any 4-digit number. Month uses alternation to enforce 01-12. Day enforces 01-31, though it won't catch month-specific overflows (e.g. Feb 30).

Examples

Input

2024-01-15

Matches

  • 2024-01-15

Input

1999-12-31

Matches

  • 1999-12-31

Input

2024-13-01

No match

Same pattern, other engines

← Back to ISO 8601 Date overview (all engines)