package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?mi)[-\[,:.\/]?\b(?:1\d{3}|200[0123]|\d0)(?:'?s)?\b[-\],:.\/]?`)
var str = `I need it to catch a date between 1000 and 2003 in these forms:
1975
1970s
1970's
1970S
1970'S
70s
70's
70S
70'S
I also need it to catch certain characters on either side of the date, including
dashes
-1973-
brackets
[1973]
commas
,1973,
colons
:1973:
periods
.1973.
slashes
/1973/
- some on both sides, some on only one.
/1973
or
1973/
My problem is that the expression is catching other characers on either side of the date as well - +1975 gets through, for instance, as does 1970s& - letters and numbers on either side do not get through, however. I'm confused.
I think I might need some sort of limit on either side before I can state the exceptions, I'm not sure what that would look like - some kind of look back? Any help would be appreciated.`
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/