Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regular expression sub-match not used in replace #42291

Closed
xrfang opened this issue Oct 30, 2020 · 1 comment
Closed

regular expression sub-match not used in replace #42291

xrfang opened this issue Oct 30, 2020 · 1 comment

Comments

@xrfang
Copy link

xrfang commented Oct 30, 2020

What version of Go are you using (go version)?

$ go version

go version go1.15 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/xrfang/.cache/go-build"
GOENV="/home/xrfang/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE="git2.likeit.cn"
GOMODCACHE="/home/xrfang/go/pkg/mod"
GONOPROXY="git2.likeit.cn"
GONOSUMDB="git2.likeit.cn"
GOOS="linux"
GOPATH="/home/xrfang/go"
GOPRIVATE="git2.likeit.cn"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build285451013=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"fmt"
	"regexp"
)

func main() {
	s := `</OCR_RVCT3>
	<OCR_RVCT4>
	  <Type>1</Type>
	  <StartAddress>0x8008000</StartAddress>
	  <Size>0x38000</Size>
	</OCR_RVCT4>
	<OCR_RVCT5>`
	rx := regexp.MustCompile(`(?s)(<OCR_RVCT4>.*?<StartAddress>)(.+?</StartAddress>)`)
	s = rx.ReplaceAllString(s, `$1TEST</StartAddress>`)
	fmt.Println(s)
}

What did you expect to see?

</OCR_RVCT3>
	<OCR_RVCT4>
	  <Type>1</Type>
	  <StartAddress>TEST</StartAddress>
	  <Size>0x38000</Size>
	</OCR_RVCT4>
	<OCR_RVCT5>

What did you see instead?

</OCR_RVCT3>
        </StartAddress>
          <Size>0x38000</Size>
        </OCR_RVCT4>
        <OCR_RVCT5>
@seankhliao
Copy link
Member

use ${1}, see the examples for ReplaceAllString

questions should be directed to the forums in https://github.com/golang/go/wiki/Questions

@xrfang xrfang closed this as completed Oct 30, 2020
@golang golang locked and limited conversation to collaborators Oct 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants