I have the same problem. The cleanest solution I came up with:
package main import ( "fmt" "regexp" "strings" ) func main() { re, _ := regexp.Compile("[az]{3}") s := "aaa bbb ccc" // Replace all strings fmt.Println(re.ReplaceAllString(s, "000")) // Replace one string found := re.FindString(s) if found != "" { fmt.Println(strings.Replace(s, found, "000", 1)) } }
Run:
$ go run test.go 000 000 000 000 bbb ccc
source share