How to split a string by multiple delimiters

白昼怎懂夜的黑 提交于 2021-01-21 06:57:05

问题


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 string "xxxxx:yyyyy:zzz.aaa.bbb.cc:dd" into below struct type

type Target struct {
    Service string
    Type    string
    Domain  string
    Plan    string
    Host    string
    Region  string
    Other   string
}

So that

Service = xxxxx 
Type = yyyyy 
Domain = zzzz 
Plan = aaa 
Host = bbb 
Region = cc 
Other = dd 

回答1:


You may use

strings.FieldsFunc(input, Split)

Try it on The Go Playground:

package main

import (
    "fmt"
    "strings"
)

func main() {
    input := `xxxxx:yyyyy:zzz.aaa.bbb.cc:dd:ee:ff`
    a := strings.FieldsFunc(input, Split)
    t := Target{a[0], a[1], a[2], a[3], a[4], a[5], a[6]}
    fmt.Println(t) // {xxxxx yyyyy zzz aaa bbb cc dd}
}
func Split(r rune) bool {
    return r == ':' || r == '.'
}

type Target struct {
    Service string
    Type    string
    Domain  string
    Plan    string
    Host    string
    Region  string
    Other   string
}

output:

{xxxxx yyyyy zzz aaa bbb cc dd}



回答2:


You can use regex for splitting your string

import "regexp"

func splitWord(word string) []string {
    array := regexp.MustCompile("[\\:\\,\\.\\s]+").Split(word, -1)
    return array
}



回答3:


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) })



回答4:


Here is a generic function that will take a string as a set of runes to split on.

func Splitter(s string, splits string) []string {
    m := make(map[rune]int)
    for _, r := range splits {
        m[r] = 1
    }

    splitter := func(r rune) bool {
        return m[r] == 1
    }

    return strings.FieldsFunc(s, splitter)
}

func TestSplit() {
    words := Splitter("orange apple-banana", " -")
}



回答5:


Alright. This isn't a very elegant solution but it should at least get you started and works for the specific example you've given. In reality you'd probably want to add some error handling or generalize the logic a bit to work with a broader set of inputs.

type Target struct {
    Service string
    Type string
    Domain string
    Plan string
    Host string
    Region string
    Other string
}

func main() {
    input := `xxxxx:yyyyy:zzz.aaa.bbb.cc:dd:ee:ff`
    t := Target{}
    tokens := strings.Split(input, ":")
    t.Service = tokens[0]
    t.Type = tokens[1]
    subtokens := strings.Split(tokens[2], ".")
    t.Domain = subtokens[0]
    t.Plan = subtokens[1]
    t.Host = subtokens[2]
    t.Region = subtokens[3]
    t.Other = tokens[3]
    fmt.Printf("%v", t)
}

Working example here; https://play.golang.org/p/57ZyOfdbvo



来源:https://stackoverflow.com/questions/39862613/how-to-split-a-string-by-multiple-delimiters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!