Replace first occurrence of string using Regexp
Example
package main
import (
"fmt"
"regexp"
)
func main() {
strEx := "Php-Golang-Php-Python-Php-Kotlin"
reStr := regexp.MustCompile("^(.*?)Php(.*)$")
repStr := "${1}Java$2"
output := reStr.ReplaceAllString(strEx, repStr)
fmt.Println(output)
}
Output
Java-Golang-Php-Python-Php-Kotlin
Most Helpful This Week
How to convert Colorful PNG image to Gray-scale?
How to check if a string contains certain characters in Golang?
Split a character string based on change of character
Regular expression to extract text between square brackets
Functions mess (recursive anonymous function) in Golang
How to copy a map to another map?