Updated function names, addeed new function 'FindString' that returns the _text_ of the match

master
Aadhavan Srinivasan 2 days ago
parent 7056026e10
commit a14ab81697

@ -138,10 +138,29 @@ func pruneIndices(indices []Match) []Match {
return toRet
}
// findNthMatch finds the 'n'th match of the regex represented by the given start-state, with
// FindString returns a _string_ containing 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 FindString(regex Reg, str string) string {
match, err := FindNthMatch(regex, str, 1)
if err != nil {
return ""
}
return str[match[0].startIdx:match[0].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 FindAllString(regex Reg, str []string) []string {
//
//}
// FindNthMatch finds the 'n'th match of the regex represented by the given start-state, with
// 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 FindNthMatch(regex Reg, str string, n int) (Match, error) {
idx := 0
matchNum := 0
str_runes := []rune(str)
@ -160,9 +179,9 @@ func findNthMatch(regex Reg, str string, n int) (Match, error) {
return nil, fmt.Errorf("Invalid match index. Too few matches found.")
}
// findAllMatches tries to find all matches of the regex represented by given start-state, with
// FindAllMatches tries to find all matches of the regex represented by given start-state, with
// the given string
func findAllMatches(regex Reg, str string) []Match {
func FindAllMatches(regex Reg, str string) []Match {
idx := 0
str_runes := []rune(str)
var matchFound bool
@ -180,7 +199,7 @@ func findAllMatches(regex Reg, str string) []Match {
return indices
}
// Helper for findAllMatches. Returns whether it found a match, the
// Helper for FindAllMatches. Returns whether it found a match, the
// first Match it finds, and how far it got into the string ie. where
// the next search should start from.
//

Loading…
Cancel
Save