Wik*_*żew 5

你可以用

(MYSTRING=).*
${1}foo
(MYSTRING=).*MYSTRING=${1}.*
package main

import (
    "fmt"
    "regexp"
)

const sample = `MYSTRING=${MYSTRING}
MYSTRING=
MYSTRING=randomstringwithvariablelength
`
func main() {
    var re = regexp.MustCompile(`(MYSTRING=).*`)
    s := re.ReplaceAllString(sample, `${1}foo`)
    fmt.Println(s)
}

输出:

MYSTRING=foo
MYSTRING=foo
MYSTRING=foo