-
-
Save elliotchance/d419395aa776d632d897 to your computer and use it in GitHub Desktop.
func main() { | |
str := "abc foo:bar def baz:qux ghi" | |
re := regexp.MustCompile("([a-z]+):([a-z]+)") | |
result := ReplaceAllStringSubmatchFunc(re, str, func(groups []string) string { | |
return groups[1] + "." + groups[2] | |
}) | |
fmt.Printf("'%s'\n", result) | |
} |
import "regexp" | |
func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string { | |
result := "" | |
lastIndex := 0 | |
for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) { | |
groups := []string{} | |
for i := 0; i < len(v); i += 2 { | |
groups = append(groups, str[v[i]:v[i+1]]) | |
} | |
result += str[lastIndex:v[0]] + repl(groups) | |
lastIndex = v[1] | |
} | |
return result + str[lastIndex:] | |
} |
See related blog post for slightly more background: http://elliot.land/post/go-replace-string-with-regular-expression-callback
Here is a go playground link (with @vruzin change): https://play.golang.org/p/yBcmsTS8Ycm
The "groups" argument to the callback is a little weird and deserves some documentation:
- The 0th element is the original full match
- The following slice elements are the nth string found by a regex parenthesized capture group (including named capturing groups)
This matches other languages to make things convenient for porting:
- PHP: preg_replace_callback($pattern, $callback, $subject)
- Ruby: subject.gsub(pattern) {|match| callback}
- Python: re.sub(pattern, callback, subject)
- JavaScript: subject.replace(pattern, callback)
However, if you are not porting, it's a little unexpected.
Golang also has ReplaceAllStringFunc() which is more limited, as it returns a copy of src in which all matches of the Regexp have been replaced by the return value of function repl applied to the matched substring. If you need to look at the surrounding text, or at the position, in order to determine what to replace it with you need something else.
bad function ))
change
on
Otherwise, if the group is not found, there will be an error.
For example:
(?:aa(xxx)bbb)?(yyy)