Home >Backend Development >Golang >Go: Split a string of comma-separated key/value pairs. Commas may be embedded in a given key/value pair.
In PHP, you can use the Go function to split a string of key/value pairs separated by commas. This function is very useful, especially when there may be embedded commas in a given key/value pair. Go functions make it easy to split key/value pairs into independent parts for further processing and use. Whether processing form data or parsing CSV files, Go functions can help us complete the task efficiently. In this article, we will detail how to split key/value pairs using Go functions and provide some practical examples for reference. let's start!
I am fairly new to the Go language and am looking for a way to split a variable length string into a map where the key/value pairs (all strings) are separated by commas delimited, only some variations of the string may have multiple commas and/or colons in a given key/value pair. For example:
String1Variation keyword1=value1,keyword2=value2,keyword3=value3,value4,value5,keyword4=value6... String2Variation Keyword 1 = value 1, keyword 2 = value 2, keyword 3 = value 3, value 4, value 5: value 6, value 7, value 8, keyword 4 = value 9...
where "..." reflects the fact that there will be a variable # key/value pair. I hope to end up with a map like this:
map[string]string{ keyword1: "value1", keyword2: "value2", keyword3: "value3,value4,value5", keyword4: "valu6", }
or
map[string]string{ keyword1: "value1", keyword2: "value2", keyword3: "value3,value4,value5:value6,value7,value8", keyword4: "valu6", }
Unfortunately, the format of the lines I will be reading from the file is quite complex, and my options for changing the format of the incoming data are limited. I've been working on this for a while, unfortunately I'm not a wizard with regular expressions, so any help would be greatly appreciated.
Match (global), not split, then you don't need to look around: (.*? )=([^=]*)( ?:,|$)
. In Go:
package kvparse import ( "regexp" "testing" "reflect" ) var kvPairRe = regexp.MustCompile(`(.*?)=([^=]*)(?:,|$)`) func ParseKV(kvStr string) map[string]string { res := map[string]string{} for _, kv := range kvPairRe.FindAllStringSubmatch(kvStr, -1) { res[kv[1]] = kv[2] } return res } func TestParseKV(t *testing.T) { test := func(kvStr string, expectedMap map[string]string) { gotMap := ParseKV(kvStr) if !reflect.DeepEqual(expectedMap, gotMap) { t.Errorf("Abs(%s) = %v; want %v", kvStr, gotMap, expectedMap) } } test("keyword1=value1,keyword2=value2,keyword3=value3,value4,value5,keyword4=value6", map[string]string{ "keyword1": "value1", "keyword2": "value2", "keyword3": "value3,value4,value5", "keyword4": "value6" }) test("keyword1=value1,keyword2=value2,keyword3=value3,value4,value5:value6,value7,value8,keyword4=value9", map[string]string{ "keyword1": "value1", "keyword2": "value2", "keyword3": "value3,value4,value5:value6,value7,value8", "keyword4": "value9" }) }
The above is the detailed content of Go: Split a string of comma-separated key/value pairs. Commas may be embedded in a given key/value pair.. For more information, please follow other related articles on the PHP Chinese website!