BenchmarkRegexp 500000 5136 ns/op
BenchmarkStrings 10000000 173 ns/op
http://play.golang.org/p/YT29Ao-tOt BenchmarkRegexp 500000 5136 ns/op
BenchmarkStrings 10000000 173 ns/op
http://play.golang.org/p/YT29Ao-tOtThe classic example is to recognize that you're looking for a fixed string, and substitute in Boyer Moore. But prefix/suffix recognition are two other common examples.
MustCompile is generally used to make the regexp a global so that it isn't done over and over.
Just move it out of the loop, as it's really not necessary to compile regular expressions every time you want to match/replace against it.
For trivial replacements string manipulation I find is faster and safer (fewer bugs). But there is some threshold of complexity in which regular expressions are both more performant and safer.
Realistically, you'd expect them to behave exactly the same, but Go's pretty new, and you never know what is or isn't going to be optimized.
`\A@(.*)@\z`
BenchmarkRegexp 500000 5181 ns/op
BenchmarkStrings 10000000 171 ns/opThe problem is when developers that don't know any better build parsers with regular expressions. That's almost always a bad idea.