package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)(?<day>0?[1-9]|[12][0-9]|3[01])(?>.{1})(?(?=0?[469]|11)(?<!31(?>.{1}))|)(?(?=0?2)(?<!3[01](?>.{1})))(?(?=0?2(?>.{1})\d\d(?!00)(?:[02468][048]|[13579][26]))|(?<!29(?>.{1})))(?<month>0?[1-9]|1[12])(?>.{1})(?<year>19[0-9][0-9]|20[0-9][0-9]|[0-9]{2,4})(?!\d)`)
var str = `29.02.1704
01.01.1987 // ok
101.01.19870 // bad- too many numbers
1.1.1987 // ok
32.02.1987 // bad- 32 feb
29.02.2001 // bad
0.0.1987 // bad
0.13.1987 // bad
8.8.8.8 // bad
31.04.1987 // bad
31.06.1987 // bad
31.09.1987 // bad
31.11.1987 // bad
30.02.1987 // bad
29.02/1987 // bad
29.02.1988 // ok
29.02.2000 // bad
29.02.2408
`
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/