2020-11-24 06:59:01 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import "regexp"
|
|
|
|
|
2022-08-24 13:12:05 +00:00
|
|
|
type RegexpSubmatcher struct {
|
2020-11-24 06:59:01 +00:00
|
|
|
re *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
2022-08-24 13:12:05 +00:00
|
|
|
// NewRegexpMatcher creates a new regexp sub matcher.
|
|
|
|
func NewRegexpMatcher(re *regexp.Regexp) *RegexpSubmatcher {
|
|
|
|
return &RegexpSubmatcher{re: re}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSubmatches returns matches from provided string. Zero length indicates no match.
|
|
|
|
func (r *RegexpSubmatcher) GetSubmatches(target string) map[string]string {
|
2020-11-24 06:59:01 +00:00
|
|
|
matches := r.re.FindStringSubmatch(target)
|
|
|
|
l := len(matches)
|
|
|
|
|
|
|
|
sub := make(map[string]string, l)
|
|
|
|
for i, name := range r.re.SubexpNames() {
|
|
|
|
if i > 0 && i <= l {
|
|
|
|
sub[name] = matches[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sub
|
|
|
|
}
|