Last active
April 3, 2024 14:47
-
-
Save eculver/d1338aa87e87890e05d4f61ed0a33d6e to your computer and use it in GitHub Desktop.
Revisions
-
eculver revised this gist
Oct 11, 2018 . 1 changed file with 4 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,10 @@ // FindAllGroups returns a map with each match group. The map key corresponds to the match group name. // A nil return value indicates no matches. func FindAllGroups(re *regexp.Regexp, s string) map[string]string { matches := re.FindStringSubmatch(s) subnames := re.SubexpNames() if matches == nil || len(matches) != len(subnames) { return nil } matchMap := map[string]string{} -
eculver revised this gist
Oct 11, 2018 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,13 +17,13 @@ func FindAllGroups(re *regexp.Regexp, s string) (map[string]string, error) { } func ExampleFindAllGroups() { re := regexp.MustCompile(`(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})`) matchMap := FindAllGroups(re, "2018-10-11") for k, v := range matchMap { fmt.Printf("%s: %s\n", k, v) } // Output: // year: 2018 // month: 10 // day: 11 } -
eculver created this gist
Oct 11, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ // FindAllGroups returns a map with each match group. The map key corresponds to the match group name. func FindAllGroups(re *regexp.Regexp, s string) (map[string]string, error) { matches := re.FindStringSubmatch(s) subnames := re.SubexpNames() if matches == nil { return nil, errors.New("no matches") } if len(matches) != len(subnames) { return nil, errors.New("wrong matches") } matchMap := map[string]string{} for i := 1; i < len(matches); i++ { matchMap[subnames[i]] = matches[i] } return matchMap, nil } func ExampleFindAllGroups() { re := regexp.MustCompile(`(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})`) matchMap := FindAllGroups(re, "2018-10-11") for k, v := range matchMap { fmt.Printf("%s: %s\n", k, v) } // Output: // year: 2018 // month: 10 // day: 11 }