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 to handle GET and POST request in Golang
Golang HTML parser
Regular expression to validate common Credit Card Numbers
How to use a mutex to define critical sections of code and fix race conditions?
How to check UPPERCASE characters in a string in Golang?
Example of Sscan vs Sscanf vs Sscanln from FMT Package