github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/easy/regexp.go (about) 1 package easy 2 3 import "regexp" 4 5 // MatchGroups returns the matched named capturing groups. 6 // A returned value of nil indicates no match. 7 func MatchGroups(re *regexp.Regexp, str []byte) map[string][]byte { 8 match := re.FindSubmatch(str) 9 if len(match) == 0 { 10 return nil 11 } 12 13 out := make(map[string][]byte, len(re.SubexpNames())-1) 14 for i, key := range re.SubexpNames() { 15 if i > 0 && key != "" { 16 out[key] = match[i] 17 } 18 } 19 return out 20 } 21 22 // MatchStringGroups returns the matched named capturing groups. 23 // A returned value of nil indicates no match. 24 func MatchStringGroups(re *regexp.Regexp, str string) map[string]string { 25 match := re.FindStringSubmatch(str) 26 if len(match) == 0 { 27 return nil 28 } 29 30 out := make(map[string]string, len(re.SubexpNames())-1) 31 for i, key := range re.SubexpNames() { 32 if i > 0 && key != "" { 33 out[key] = match[i] 34 } 35 } 36 return out 37 }