Golang regexp.ReplaceAllFunc() function example
package regexp
Golang regexp.ReplaceAllFunc() function usage example.
NOTE : The inner function is useful in cases where you need to evaluate certain data first to extract the replacement string.
package main
import (
"fmt"
"regexp"
)
func main() {
// regular expression pattern
regE := regexp.MustCompile("[a-c]*$")
src := []byte("abcdabc")
//repl := []byte("x")
//newByte := regE.ReplaceAll(src, repl)
// NOTE : The inner function is useful in cases where you need to evaluate certain data first
// to extract the replacement string.
// In this example, we will just use string "x" for simplicity sake
newByte := regE.ReplaceAllFunc([]byte(src), func(s []byte) []byte { return []byte("x") })
fmt.Println(string(newByte))
}
Output :
abcdx
References :
See also : Golang regexp.ReplaceAll() function example
Advertisement
Something interesting
Tutorials
+6.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+5.2k Golang : Calculate half life decay example
+5.4k Python : Delay with time.sleep() function example
+7.3k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+26.8k Golang : Convert file content into array of bytes
+9.6k Javascript : Read/parse JSON data from HTTP response
+10.1k Golang : Print how to use flag for your application example
+8.8k Android Studio : Image button and button example
+22.3k Golang : Read directory content with filepath.Walk()
+8.7k Golang : How to join strings?
+9.7k Golang : Eroding and dilating image with OpenCV example
+17.9k Golang : How to make a file read only and set it to writable again?