exregexp-go is a Go package that extends the standard regexp library by providing utilities for flexible string replacement using regular expression submatches.
This package introduces the ReplaceAllSubmatchFunc and the ReplaceAllStringSubmatchFunc function, which allows you to leverage capturing groups for custom replacement logic.
- Flexible string replacement using capturing groups
- Simple and intuitive API
- Built on top of Go's standard
regexplibrary
go get -u github.com/hymkor/exregexp-goThe following example demonstrates how to use exregexp.ReplaceAllStringSubmatchFunc:
package main
import (
"fmt"
"regexp"
"github.com/hymkor/exregexp-go"
)
func main() {
re := regexp.MustCompile(`\b([a-zA-Z]+)(\d+)\b`)
input := "example123 test456 hello789"
output := exregexp.ReplaceAllStringSubmatchFunc(re, input, func(submatches []string) string {
return fmt.Sprintf("%s(%s)", submatches[1], submatches[2])
})
fmt.Println(output)
}example(123) test(456) hello(789)