Skip to content

Instantly share code, notes, and snippets.

@wader
Created December 2, 2017 11:03
Show Gist options
  • Save wader/bd548cc5f20cc8650cc7739832d76db5 to your computer and use it in GitHub Desktop.
Save wader/bd548cc5f20cc8650cc7739832d76db5 to your computer and use it in GitHub Desktop.
go regexp replace capture group
func replace(content string, re *regexp.Regexp, s string) (string, string) {
v := re.FindStringSubmatchIndex(content)
if v == nil {
return content, ""
}
p0 := v[2]
p1 := v[3]
return content[0:p0] + s + content[p1:], content[p0:p1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment