Skip to content

Instantly share code, notes, and snippets.

@eculver
Last active April 3, 2024 14:47
Show Gist options
  • Save eculver/d1338aa87e87890e05d4f61ed0a33d6e to your computer and use it in GitHub Desktop.
Save eculver/d1338aa87e87890e05d4f61ed0a33d6e to your computer and use it in GitHub Desktop.

Revisions

  1. eculver revised this gist Oct 11, 2018. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions regex-utils.go
    Original 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.
    func FindAllGroups(re *regexp.Regexp, s string) (map[string]string, error) {
    // 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 {
    return nil, errors.New("no matches")
    }
    if len(matches) != len(subnames) {
    return nil, errors.New("wrong matches")
    if matches == nil || len(matches) != len(subnames) {
    return nil
    }

    matchMap := map[string]string{}
  2. eculver revised this gist Oct 11, 2018. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions regex-utils.go
    Original 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")
    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
    // Output:
    // year: 2018
    // month: 10
    // day: 11
    }
  3. eculver created this gist Oct 11, 2018.
    29 changes: 29 additions & 0 deletions regex-utils.go
    Original 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
    }