I'm trying to find a good way to split a string using a regular expression instead of a string. Thanks
6 Answers
You can use regexp.Split
to split a string into a slice of strings with the regex pattern as the delimiter.
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile("[0-9]+")
txt := "Have9834a908123great10891819081day!"
split := re.Split(txt, -1)
set := []string{}
for i := range split {
set = append(set, split[i])
}
fmt.Println(set) // ["Have", "a", "great", "day!"]
}

- 507
- 4
- 15

- 14,023
- 7
- 43
- 39
-
1This helped me a lot. – Jonathan Kittell Dec 19 '19 at 19:41
I made a regex-split function based on the behavior of regex split function in java, c#, php.... It returns only an array of strings, without the index information.
func RegSplit(text string, delimeter string) []string {
reg := regexp.MustCompile(delimeter)
indexes := reg.FindAllStringIndex(text, -1)
laststart := 0
result := make([]string, len(indexes) + 1)
for i, element := range indexes {
result[i] = text[laststart:element[0]]
laststart = element[1]
}
result[len(indexes)] = text[laststart:len(text)]
return result
}
example:
fmt.Println(RegSplit("a1b22c333d", "[0-9]+"))
result:
[a b c d]

- 201
- 2
- 5
The regexp.Split() function would be the best way to do this.

- 75,165
- 16
- 143
- 189
-
Looks like this was [added in Go 1.1](https://golang.org/doc/go1.1) ([May 2013](https://golang.org/doc/devel/release.html#go1.1)). – n8henrie Jul 15 '17 at 15:17
If you just want to split on certain characters, you can use strings.FieldsFunc
, otherwise I'd go with regexp.FindAllString
.

- 901
- 6
- 17
-
I tried this, but it overwrites the character I want to split on. I don't want to lose the character, just insert a space. I used a for loop to get to my goal. Thanks. PS - your answer didn't include regex, maybe that's why there's a down vote? – Brenden Nov 15 '13 at 09:45
-
Nothing is getting "overwritten", it's just a question of what was returned. If you like, you can always use a different regexp function to get the indices and use those. – Anschel Schaffer-Cohen Jan 24 '14 at 23:33
You should be able to create your own split function that loops over the results of RegExp.FindAllString, placing the intervening substrings into a new array.
http://nsf.github.com/go/regexp.html?m:Regexp.FindAllString!

- 2,314
- 2
- 23
- 17
-
1Not sure where you got `RegExp` from, but the correct package name is `regexp`. Go is case-sensitive! – Anschel Schaffer-Cohen Feb 25 '11 at 17:52
I found this old post while looking for an answer. I'm new to Go but these answers seem overly complex for the current version of Go. The simple function below returns the same result as those above.
package main
import (
"fmt"
"regexp"
)
func goReSplit(text string, pattern string) []string {
regex := regexp.MustCompile(pattern)
result := regex.Split(text, -1)
return result
}
func main() {
fmt.Printf("%#v\n", goReSplit("Have9834a908123great10891819081day!", "[0-9]+"))
}

- 103
- 1
- 7