Skip to content

regexp: confusing ReplaceAllString result when using patterns like $1_$2 #68712

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
zhouchi9 opened this issue Aug 2, 2024 · 5 comments
Closed

Comments

@zhouchi9
Copy link

zhouchi9 commented Aug 2, 2024

Go version

go version go1.20.3 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\zhouchi9\AppData\Local\go-build
set GOENV=C:\Users\zhouchi9\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\CodeEnv\Go\gopath\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\CodeEnv\Go\gopath
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\CodeEnv\Go\Go1.20.3
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\CodeEnv\Go\Go1.20.3\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.3
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\zhouchi9\AppData\Local\Temp\go-build2519956868=/tmp/go-build -gno-record-gcc-switches

What did you do?

我想将驼峰的字符串中间插入一个下划线,可是并没有达到预期

问题复现链接

What did you see happen?

输入参数:cNcHDhCh
程序结果:NHDCh

What did you expect to see?

预期:c_Nc_HDh_Ch

@gabyhelp
Copy link

gabyhelp commented Aug 2, 2024

@zhouchi9 zhouchi9 changed the title go regex 捕获分组替换 regexp 捕获分组替换 Aug 2, 2024
@ALTree
Copy link
Member

ALTree commented Aug 2, 2024

From the Expand documentation:

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.

Since the underscore _ is a valid character for an $-name, Expand is seeing two $-variables: one named $1_ and a numeric one ($2). The second works, while the first doesn't refer to a valid named submatch, so the [a-z] part of the match is simply not included in the replacement.

Unfortunately I think it's not possible to insert a _ character in this way. You could use "$1 _$2" (the space prevents $1_ from being recognized as a single token) and then do a second pass on the string to strip the spaces before the _.

Anyway I think this is working as documented.

@ALTree ALTree changed the title regexp 捕获分组替换 regexp: confusing ReplaceAllString result when using patterns like $1_$2 Aug 2, 2024
@tdakkota
Copy link

tdakkota commented Aug 2, 2024

It is possible to insert _ via ${1}_${2}.

https://go.dev/play/p/3GUENtz8VQA

@zhouchi9
Copy link
Author

zhouchi9 commented Aug 2, 2024

谢谢, 看了文档,做了下改动 ${1}_${2}

@ALTree
Copy link
Member

ALTree commented Aug 2, 2024

It is possible to insert _ via ${1}_${2}.

https://go.dev/play/p/3GUENtz8VQA

Nice! Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants