package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?im)(?(DEFINE)
(?<int>
(?:
\G|
(?!\n)
)
\s*\b\d{1,5}\s*
)
(?<timepart>
(
s|
m|
h|
d
)
\b
)
)
^
((?&int))
((?&timepart))
(?<string>.+)
$`)
var str = `2min baz
12 mins foo
1 hour 20 mins remind this
1sec
5 hours
1 sec foo
3 minutes 4 seconds
3333 sec do
1 hrs 20 mins 5 seconds remind this
2 days 3 hrs 20 mins 5 seconds remind this
foo 2sec bar //fail
2 s hi
3 m foo
3m bar
2d baz
9 h foobar`
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/