How to use regular expression to validate the date format dd/mm/yyyy in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"regexp"
)

func main() {
    // dd/mm/yyyy
	s1 := "31/08/2020"
	s2 := "1/13/2020"
	s3 := "1/12/2020"
	s4 := "29/2/2020"
	s5 := "31/08/20"
	s6 := "31/08/202z"

	re := regexp.MustCompile("(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)")

	fmt.Printf("%v : %v\n", s1, re.MatchString(s1))
	fmt.Printf("%v : %v\n", s2, re.MatchString(s2))
	fmt.Printf("%v : %v\n", s3, re.MatchString(s3))
	fmt.Printf("%v : %v\n", s4, re.MatchString(s4))
	fmt.Printf("%v : %v\n", s5, re.MatchString(s5))
	fmt.Printf("%v : %v\n", s6, re.MatchString(s6))
}
   
   
 
/*
run:
  
31/08/2020 : true
1/13/2020 : false
1/12/2020 : true
29/2/2020 : true
31/08/20 : false
31/08/202z : false
  
*/

 



answered Aug 16, 2020 by avibootz

Related questions

1 answer 84 views
1 answer 83 views
1 answer 134 views
1 answer 210 views
...