package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)/(?<resource>[\w\-]+)(?:/(?<id>(?!actions)[\w\-]+))?(?:/actions/(?<action>[\w\-]+))?`)
var str = `/resource
/resource/1
/resource/1/sub-resource
/resource/1/sub-resource/2
/resource/1/sub-resource/2/sub-sub-resource
/resource/1/sub-resource/2/sub-sub-resource/3
/resource/actions/complete
/resource/1/actions/complete
/resource/1/sub-resource/actions/complete
/resource/1/sub-resource/2/actions/complete
/resource/1/sub-resource/2/sub-sub-resource/actions/complete
/resource/1/sub-resource/2/sub-sub-resource/3/actions/complete`
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/