Skip to content

regexp: issue parse string #68940

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
Alter-stack opened this issue Aug 19, 2024 · 3 comments
Closed

regexp: issue parse string #68940

Alter-stack opened this issue Aug 19, 2024 · 3 comments

Comments

@Alter-stack
Copy link

Alter-stack commented Aug 19, 2024

Go version

go1.20

Output of go env in your module/workspace:

GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/Users/xxxx/go1.20/go1.20"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/Users/xxxx/go1.20/go1.20/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.20"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
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 x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/jl/_cckn_y96kg7wnz6t3624xkr0000gn/T/go-build4144142847=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

func main() {
	pattern := fmt.Sprintf(`\b%s\b`, "ch")
	regex := regexp.MustCompile(pattern)
	result := regex.ReplaceAllStringFunc("chácara", func(match string) string {
		trimmedMatch := strings.TrimSpace(match)
		replacement := "replacement"
		return strings.ReplaceAll(match, trimmedMatch, replacement)
	})
	fmt.Println(result)
}

What did you see happen?

actually result is "replacementácara"

What did you expect to see?

except result is "chácara"

@Alter-stack
Copy link
Author

sorry, code is:

func main() {
	pattern := fmt.Sprintf(`\b%s\b`, "ch")

	regex := regexp.MustCompile(pattern)
	result := regex.ReplaceAllStringFunc("chácara", func(match string) string {
		trimmedMatch := strings.TrimSpace(match)
		replacement := "replacement"
		return strings.ReplaceAll(match, trimmedMatch, replacement)
	})
	fmt.Println(result)
}

@seankhliao
Copy link
Member

see https://github.com/google/re2/wiki/Syntax
word boundaries use only ascii

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 19, 2024
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

3 participants