Regular expression to validate the date format in "dd/mm/yyyy"
Example
package main
import (
"fmt"
"regexp"
)
func main() {
str1 := "31/07/2010"
str2 := "1/13/2010"
str3 := "29/2/2007"
str4 := "31/08/2010"
str5 := "29/02/200a"
str6 := "29/02/200a"
str7 := "55/02/200a"
str8 := "2_/02/2009"
re := regexp.MustCompile("(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)")
fmt.Printf("Pattern: %v\n", re.String()) // print pattern
fmt.Printf("\nDate: %v :%v\n", str1, re.MatchString(str1))
fmt.Printf("Date: %v :%v\n", str2, re.MatchString(str2))
fmt.Printf("Date: %v :%v\n", str3, re.MatchString(str3))
fmt.Printf("Date: %v :%v\n", str4, re.MatchString(str4))
fmt.Printf("Date: %v :%v\n", str5, re.MatchString(str5))
fmt.Printf("Date: %v :%v\n", str6, re.MatchString(str6))
fmt.Printf("Date: %v :%v\n", str7, re.MatchString(str7))
fmt.Printf("Date: %v :%v\n", str8, re.MatchString(str8))
}
Output
Pattern: (0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\d\d)
Date: 31/07/2010 :true
Date: 1/13/2010 :false
Date: 29/2/2007 :true
Date: 31/08/2010 :true
Date: 29/02/200a :false
Date: 29/02/200a :false
Date: 55/02/200a :false
Date: 2_/02/2009 :false
Most Helpful This Week
How to declare empty Map in Go?
Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]
Golang String Concatenation
How to check if a map contains a key in Go?
Regex to extract image name from HTML in Golang
How to fix race condition using Atomic Functions in Golang?
Regular expression to extract filename from given path in Golang
Functions mess (recursive anonymous function) in Golang
Get Year, Month, Day, Hour, Min and Second from a specified date
How to wait for Goroutines to Finish Execution?