您可以使用
(MYSTRING=).*
并替换为
${1}foo。请参阅在线Go regex演示。在这里,
(MYSTRING=).*匹配并捕获
MYSTRING=子字符串(
${1}它将从替换模式中引用此值),.*并将匹配并消耗除换行符以外的任何0+字符,直至行尾。
参见Go演示:
package mainimport ( "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=fooMYSTRING=fooMYSTRING=foo



