Skip to content

regexp: issue parse string #68940

Closed as not planned
Closed as not planned
@Alter-stack

Description

@Alter-stack

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions