package regexp2import ()// Split splits the given input string using the pattern and returns// a slice of the parts. Count limits the number of matches to process.// If Count is -1, then it will process the input fully.// If Count is 0, returns nil. If Count is 1, returns the original input.// The only expected error is a Timeout, if it's set.//// If capturing parentheses are used in the Regex expression, any captured// text is included in the resulting string array// For example, a pattern of "-" Split("a-b") will return ["a", "b"]// but a pattern with "(-)" Split ("a-b") will return ["a", "-", "b"]func ( *Regexp) ( string, int) ([]string, error) {if < -1 {returnnil, errors.New("count too small") }if == 0 {returnnil, nil }if == 1 {return []string{}, nil }if == -1 {// no limit = math.MaxInt }// iterate through the matches := 0var []stringvar []rune , := .FindStringMatch()for ; != nil && > 0; , = .FindNextMatch() { = .text.runes// if we have an m, we don't have an err // append our match = append(, string([:.RuneIndex]))// append any capture groups, skipping group 0 := .Groups()for := 1; < len(); ++ { = append(, [].String()) } = .RuneIndex + .RuneLength -- }if != nil {returnnil, }if == nil {// we never matched, return the original stringreturn []string{}, nil }// append our remainder = append(, string([:]))return , nil}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.