How to check if time format HH:MM is correct with regular expression in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"regexp"
)

func main() {
	s1 := "9:35"
	s2 := "13:53"
	s3 := "23:59"
	s4 := "0:03"
	s5 := "24:59"

	re := regexp.MustCompile(`^([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9])$`)

	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))
}



/*
run:

9:35 - true
13:53 - true
23:59 - true
0:03 - true
24:59 - false

*/

 



answered Aug 24, 2020 by avibootz

Related questions

...