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 Make HTTP Requests in Go?
How to read/write from/to file in Golang?
Replace numbers by zero from string
Golang download image from given URL
What is Rune? How to get ASCII value of any character in Go?
How to convert Go struct to JSON?
Golang HTML parser
How to Unmarshal nested JSON structure?
Higher Order Functions in Golang
Create and Print Multi Dimensional Slice in Golang