I want to parse a string xxxxx:yyyyy:zzz.aaa.bbb.cc:dd:ee:ff to a struct in Go, how can I do it with multiple delimiter \':\' and \'.\'.
Edit:
I want to split the
You can use this function, which can split a string by multiple runes:
import "fmt"
import "strings"
func SplitAny(s string, seps string) []string {
splitter := func(r rune) bool {
return strings.ContainsRune(seps, r)
}
return strings.FieldsFunc(s, splitter)
}
func main() {
words := SplitAny("xxxxx:yyyyy:zzz.aaa.bbb.cc:dd:ee:ff", ".:")
fmt.Println(strings.Join(words, " "))
}
Output:
xxxxx yyyyy zzz aaa bbb cc dd ee ff
Or even with one line of code:
words := strings.FieldsFunc(s, func(r rune) bool { return strings.ContainsRune(" .:", r) })