I am using string.Replace to replace substring A
func removeIP(text string) string {
text = strings.Replace(text, "someWord", "**NewWord**", -1)
return text
}
func removeIPUsingRegex(text string) string {
var re = regexp.MustCompile(`\b` + "someWord" + `\b`) // I want to replace whole word only
text = re.ReplaceAllString(text, "**NewWord**")
}
The Issue I am facing here is, I want to replace a whole word only if is not supported by string replace.
As I have to replace for very very huge strings may be in GBs. Regex is very slow compare to string replace.
eg: text: "abcdef defgh /def/ .def/ =def= def xxxy" -> Replace def with DEF
output: "abcdef defgh /DEF/ .DEF/ =DEF= DEF xxxy" //Notice only whole words have been replaced.
Regex bumps the time by almost 100 times (https://medium.com/codezillas/golang-replace-vs-regexp-de4e48482f53). Any Ideas will be much appreciated.
strings.Replaceandregexp.ReplaceAllString, but it would also require more effort in development and testing.io.Reader, you could also look at this question about replacing characters while streaming, it however doesn't answer the "whole word" thing