我有以下可能形式的字符串:
MYSTRING=${MYSTRING}\n
MYSTRING=\n
MYSTRING=randomstringwithvariablelength\n
MYSTRING=fooMYSTRING=\n
re := regexp.MustCompile("MYSTRING=*\n")
s = re.ReplaceAllString(s, "foo")
但它不起作用.任何帮助表示赞赏.
\n1> Wiktor Strib..:
你可以用
(MYSTRING=).*
${1}foo
(MYSTRING=).*MYSTRING=${1}.*
参见Go演示:
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