Skip to content

regex: ReplaceAllString and ReplaceAll broken group replacement #65000

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

Closed
yunier-rojas opened this issue Jan 7, 2024 · 3 comments
Closed

regex: ReplaceAllString and ReplaceAll broken group replacement #65000

yunier-rojas opened this issue Jan 7, 2024 · 3 comments

Comments

@yunier-rojas
Copy link

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

@yunier-rojas
Copy link
Author

in python at least this is the expected result:

import re

re.sub('(foo)', '\\1bar', "foo")

# foobar

@cespare
Copy link
Contributor

cespare commented Jan 7, 2024

This is working as documented. Inside the documentation for the function you called, ReplaceAllString, it says:

Inside repl, $ signs are interpreted as in Expand,

and then the Expand documentation says:

In the template, a variable is denoted by a substring of the form $name or ${name}, where name is a non-empty sequence of letters, digits, and underscores. A purely numeric name like $1 refers to the submatch with the corresponding index; other names refer to capturing parentheses named with the (?P...) syntax. A reference to an out of range or unmatched index or a name that is not present in the regular expression is replaced with an empty slice.

In the $name form, name is taken to be as long as possible: $1x is equivalent to ${1x}, not ${1}x, and, $10 is equivalent to ${10}, not ${1}0.

So you want to write ${1}bar.baz: https://go.dev/play/p/a222CVP2XB7

@cespare cespare closed this as not planned Won't fix, can't repro, duplicate, stale Jan 7, 2024
@yunier-rojas
Copy link
Author

Thanks @cespare for your response.

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