https://github.com/icholy/replace
Streaming text replacement for Go
https://github.com/icholy/replace
go golang replace-text
Last synced: 7 months ago
JSON representation
Streaming text replacement for Go
- Host: GitHub
- URL: https://github.com/icholy/replace
- Owner: icholy
- License: mit
- Created: 2020-07-16T19:57:25.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-09-07T17:42:35.000Z (over 3 years ago)
- Last Synced: 2025-08-17T06:26:07.032Z (7 months ago)
- Topics: go, golang, replace-text
- Language: Go
- Homepage:
- Size: 61.5 KB
- Stars: 71
- Watchers: 1
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Streaming text replacement
[](https://godoc.org/github.com/icholy/replace)
> This package provides a [x/text/transform.Transformer](https://godoc.org/golang.org/x/text/transform#Transformer)
> implementation that replaces text
## Example
``` go
package main
import (
"io"
"os"
"regexp"
"github.com/icholy/replace"
)
func main() {
f, _ := os.Open("file")
defer f.Close()
r := replace.Chain(f,
// simple replace
replace.String("foo", "bar"),
replace.Bytes([]byte("thing"), []byte("test")),
// remove all words that start with baz
replace.Regexp(regexp.MustCompile(`baz\w*`), nil),
// surround all words with parentheses
replace.RegexpString(regexp.MustCompile(`\w+`), "($0)"),
// increment all numbers
replace.RegexpStringFunc(regexp.MustCompile(`\d+`), func(match string) string {
x, _ := strconv.Atoi(match)
return strconv.Itoa(x+1)
}),
)
_, _ = io.Copy(os.Stdout, r)
}
```
## Notes Regexp* functions
* `RegexpTransformer` is stateful and cannot be used concurrently.
* The `replace` functions should not save or modify any `[]byte` parameters they recieve.
* If a match is longer than `MaxMatchSize` it may be skipped (Default 2kb).
* For better performance, reduce the `MaxMatchSize` size to the largest possible match.
* Do not use with [transform.Chain](https://pkg.go.dev/golang.org/x/text/transform#Chain), see https://github.com/golang/go/issues/49117.