package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)^(P((?<numYears>[1-9]+[0-9]*)Y)?((?<numMonths>[1-9]+[0-9]*)M)?((?<numDays>[1-9]+[0-9]*)D)?(T((?<numHours>[1-9]+[0-9]*)H)?((?<numMinutes>[1-9]+[0-9]*)M)?((?<numSeconds>[1-9]+[0-9]*)S)?)?|P(?<numWeeks>[1-9]+[0-9]*)W)$`)
var str = `P1Y2M3DT4H5M6S
P1M
PT1M
P1MT1M
P1W
P1D
PT2S
`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Golang, please visit: https://golang.org/pkg/regexp/