Regular expression for matching HH:MM time format in Golang
Example
package main
import (
"fmt"
"regexp"
)
func main() {
str1 := "8:2"
str2 := "9:9"
str3 := "12:29"
str4 := "02:5"
str5 := "23:59"
str6 := "55:59"
str7 := "0:01"
re := regexp.MustCompile(`^([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9])$`)
fmt.Printf("Pattern: %v\n", re.String()) // print pattern
fmt.Printf("Time: %v\t:%v\n", str1, re.MatchString(str1))
fmt.Printf("Time: %v\t:%v\n", str2, re.MatchString(str2))
fmt.Printf("Time: %v\t:%v\n", str3, re.MatchString(str3))
fmt.Printf("Time: %v\t:%v\n", str4, re.MatchString(str4))
fmt.Printf("Time: %v\t:%v\n", str5, re.MatchString(str5))
fmt.Printf("Time: %v\t:%v\n", str6, re.MatchString(str6))
fmt.Printf("Time: %v\t:%v\n", str7, re.MatchString(str7))
}
Output
Pattern: ^([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9])$
Time: 8:2 :true
Time: 9:9 :true
Time: 12:29 :true
Time: 02:5 :true
Time: 23:59 :true
Time: 55:59 :false
Time: 0:01 :true
Most Helpful This Week
Golang download image from given URL
Find out how many logical processors used by current process
Converting Int data type to Float in Go
Example to compare Println vs Printf
Golang HTML parser
Regular expression to extract DNS host-name or IP Address from string
Example: Split, Join, and Equal from BYTES Package
How to verify a string only contains letters, numbers, underscores, and dashes in Golang?
How to play and pause execution of goroutine?
Example of Switch Case with Break in For Loop