diff --git a/regex/matching.go b/regex/matching.go index 66fcfd8..fd17ea7 100644 --- a/regex/matching.go +++ b/regex/matching.go @@ -57,6 +57,11 @@ func (g Group) isValid() bool { return g.StartIdx >= 0 && g.EndIdx >= 0 } +// Simple function, makes it easier to map this over a list of matches +func getZeroGroup(m Match) Group { + return m[0] +} + // takeZeroState takes the 0-state (if such a transition exists) for all states in the // given slice. It returns the resulting states. If any of the resulting states is a 0-state, // the second ret val is true. @@ -138,30 +143,51 @@ func pruneIndices(indices []Match) []Match { return toRet } -// FindString returns a string containing the text of the leftmost match of the regex in the given string. +// Find returns the 0-group of the leftmost match of the regex in the given string. +// An error value != nil indicates that no match was found. +func (regex Reg) Find(str string) (Group, error) { + match, err := regex.FindNthMatch(str, 1) + if err != nil { + return Group{}, nil + } + return getZeroGroup(match), nil +} + +// FindAll returns a slice containing all the 0-groups of the regex in the given string. +// A 0-group represents the match without any submatches. +func (regex Reg) FindAll(str string) []Group { + indices := regex.FindAllSubmatch(str) + zeroGroups := funcMap(indices, getZeroGroup) + return zeroGroups +} + +// FindString returns the text of the leftmost match of the regex in the given string. // The return value will be an empty string in two situations: // 1. No match was found // 2. The match was an empty string func (regex Reg) FindString(str string) string { - match, err := FindNthMatch(regex, str, 1) + match, err := regex.FindNthMatch(str, 1) if err != nil { return "" } - return str[match[0].StartIdx:match[0].EndIdx] + zeroGroup := getZeroGroup(match) + return str[zeroGroup.StartIdx:zeroGroup.EndIdx] } // FindAllString is the 'all' version of FindString. // It returns a slice of strings containing the text of all matches of // the regex in the given string. func (regex Reg) FindAllString(str string) []string { - return []string{} - // matches := FindAllMatches, str string) + zerogroups := regex.FindAll(str) + matchStrs := funcMap(zerogroups, func(g Group) string { + return str[g.StartIdx:g.EndIdx] + }) + return matchStrs } -// FindNthMatch finds the 'n'th match of the regex represented by the given start-state, with -// the given string. +// FindNthMatch return the 'n'th match of the regex in the given string. // It returns an error (!= nil) if there are fewer than 'n' matches in the string. -func FindNthMatch(regex Reg, str string, n int) (Match, error) { +func (regex Reg) FindNthMatch(str string, n int) (Match, error) { idx := 0 matchNum := 0 str_runes := []rune(str) @@ -180,16 +206,6 @@ func FindNthMatch(regex Reg, str string, n int) (Match, error) { return nil, fmt.Errorf("invalid match index - too few matches found") } -// FindAll returns a slice containing all the 0-groups of the regex in the given string. -// A 0-group represents the match without any submatches. -func (regex Reg) FindAll(str string) []Group { - indices := regex.FindAllSubmatch(str) - zeroGroups := funcMap(indices, func(m Match) Group { - return m[0] - }) - return zeroGroups -} - // FindAllSubmatch returns a slice of matches in the given string. func (regex Reg) FindAllSubmatch(str string) []Match { idx := 0