Skip to content

regex: ReplaceAllString and ReplaceAll broken group replacement #65000

Closed as not planned
@yunier-rojas

Description

@yunier-rojas

Go version

go version go1.20.2 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE="on"
GOARCH="arm64"
GOBIN=""
GOCACHE="~/Library/Caches/go-build"
GOENV="~/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="~/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="~/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/yunier/go/go1.20.2"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="~/go/go1.20.2/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="~/sample/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/tr/jxpk2zl51n9d_tkg9yw439nm0000gp/T/go-build3831294269=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

When replacing a string with a match group, if any text is put after the replacement, all text is ignore afterwards. regex replacements work as expected for $1 bar while $1bar does not.

bug example

What did you see happen?

When using regex replacements, if any word (\w+) is used after the replacement, the complete text is ignored

re := regexp.MustCompile(`(foo)`)
s := re.ReplaceAllString("foo", "$1bar.baz")

outputs: .baz

What did you expect to see?

Given that the above example, I would expect it to return foobar.baz

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions