yarr/src/router/match.go
2021-04-02 22:26:43 +01:00

24 lines
637 B
Go

package router
import "regexp"
func regexGroups(input string, regex *regexp.Regexp) map[string]string {
groups := make(map[string]string)
matches := regex.FindStringSubmatchIndex(input)
for i, key := range regex.SubexpNames()[1:] {
groups[key] = input[matches[i*2+2]:matches[i*2+3]]
}
return groups
}
func routeRegexp(route string) *regexp.Regexp {
chunks := regexp.MustCompile(`[\*\:]\w+`)
output := chunks.ReplaceAllStringFunc(route, func(m string) string {
if m[0:1] == `*` {
return "(?P<" + m[1:] + ">.+)"
}
return "(?P<" + m[1:] + ">[^/]+)"
})
output = "^" + output + "$"
return regexp.MustCompile(output)
}