I\'ve noticed a lot of little debates about when to use regex and when to use a built in string function like String.Replace() (.NET).
It seems a lot of people recom
Built-in string replace method is faster than the regex when you want to replace substrings. Here are the benchmark numbers in Golang, I have tried to replace 3 type substrings.
Benchmark Replace 2 --- 236 ns/op
Benchmark Replace 5 --- 249 ns/op
Benchmark Replace 10 --- 871 ns/op
Benchmark Regexp 2 --- 3750 ns/op
Benchmark Regexp 5 --- 4457 ns/op
Benchmark Regexp 10 --- 6020 ns/op
As you can see Replace is far better than Regexp if you are replacing known substrings. But if you have to match unknown strings or patterns then regexp might be better.