golang正则表达式替换字符中含有$,结果替换失败,求教原因?

我的目的是替换文本中一部分文字内容
我要替换上去的内容 含有$特殊字符
但是替换结果显示不出来这个特殊字符
我知道$是正则的元字符 但是是替换内容 感觉应该不影响
尝试了原始字符串
但实在不知道原因在哪

代码图11.png

    src:="hello rehere world"
    tempstring:="rptID=\"DEV/LLN0$RP$urcbMeasure1\""
    tempstring2:=`rptID="DEV/LLN0$RP$urcbMeasure1"`
    reg := regexp.MustCompilePOSIX(`rehere`)
    out:=reg.ReplaceAllString(src,tempstring)
    out2:=reg.ReplaceAllString(src,tempstring2)
    fmt.Println(out)
    fmt.Println(out2)`

输出结果
22.png
这里输出了 $后面的字符全没有了 求指教

阅读 2.7k
1 个回答

ReplaceAllString:

ReplaceAllString returns a copy of src, replacing matches of the Regexp with the replacement string repl. Inside repl, $ signs are interpreted as in Expand, so for instance $1 represents the text of the first submatch.

Expand

To insert a literal $ in the output, use $$ in the template.

$ 在替换文本里有特殊含义,要输出 $ 需要用 $$

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题