US Date Format (MM/DD/YYYY) in GO
Match US-style dates in MM/DD/YYYY format with range validation.
Try it in the GO tester →Pattern
regexGO
(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4} (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4}`)
input := `01/15/2024`
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
Month alternation covers 01-12, day covers 01-31, year is any 4-digit number. Slashes are literal separators escaped with \.
Examples
Input
01/15/2024Matches
01/15/2024
Input
12/31/1999Matches
12/31/1999
Input
13/01/2024No match
—