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
Example of Switch Case with Break in For Loop
How to fix race condition using Atomic Functions in Golang?
How to replace emoji characters in string using regex in Golang?
Various examples of printing and formatting in Golang
How to create a photo gallery in Go?
How to check string contains uppercase lowercase character in Golang?