How to split '_' with one more any one character in golang?

Viewed 95

How to split '_' with any one character?

for Examples,

var one = 6221c62c67bc2a98ec6f713b_h32 
-> strings.Split(one, "_h")

var one = 12345c62c67bc2a98ec6f723c_c32
-> strings.Split(one, "_c")

var one = 12345c62c67bc2a98ec6f723c_s32
-> strings.Split(one, "_s")

How to combine these? (use regex? or any idea)

strings.Split(one, "_??")
2 Answers

You can use the regex: _[a-zA-Z], for eg

str := "6221c62c67bc2a98ec6f713b_h32"
a := regexp.MustCompile("_[a-zA-Z]")
fmt.Println(a.Split(str, 2))

You can use string.Replace:

strings.Replace("6221c62c67bc2a98ec6f713b_h32", "_h", " ", 1)
Related