Let's say I have strings like
input := `bla bla b:foo="hop" blablabla b:bar="hu?"`
and I want to replace the parts between quotes in b:foo="hop"
or b:bar="hu?"
using a function.
It's easy to build a regular expression to get the match and submatch, for example
r := regexp.MustCompile(`\bb:\w+="([^"]+)"`)
and then to call ReplaceAllStringFunc
but the problem is that the callback receives the whole match and not the submatch :
fmt.Println(r.ReplaceAllStringFunc(input, func(m string) string {
// m is the whole match here. Damn.
}))
How can I replace the submatch ?
Right now, I haven't found a better solution than to decompose myself m
inside the callback with a regex, and to rebuild the string after having processed the submatch.
I would have used an alternate approach with a positive look behind were they available in Go but that's not the case (and they shouldn't be necessary anyway).
What can I do here?
EDIT : here's my current solution that I would like to simplify :
func complexFunc(s string) string {
return "dbvalue("+s+")" // this could be more complex
}
func main() {
input := `bla bla b:foo="hop" blablabla b:bar="hu?"`
r := regexp.MustCompile(`(\bb:\w+=")([^"]+)`)
fmt.Println(r.ReplaceAllStringFunc(input, func(m string) string {
parts := r.FindStringSubmatch(m)
return parts[1] + complexFunc(parts[2])
}))
}
What bothers me is that I have to apply the regex twice. This doesn't sound right.