ISO 8601 Date-Time in GO
Match full ISO 8601 date-times with timezone offset or Z suffix (e.g. 2024-01-15T14:30:00Z).
Try it in the GO tester →Pattern
regexGO
\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+\-]\d{2}:?\d{2}) (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+\-]\d{2}:?\d{2})`)
input := `2024-01-15T14:30:00Z`
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
Date YYYY-MM-DD, literal T, time HH:MM:SS, optional fractional seconds, then either Z or a ±HH:MM / ±HHMM offset.
Examples
Input
2024-01-15T14:30:00ZMatches
2024-01-15T14:30:00Z
Input
2024-03-01T09:00:00.123+05:30Matches
2024-03-01T09:00:00.123+05:30
Input
2024-01-15No match
—