Skip to content

Regex: Underscore (_) makes replace group not found #32885

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
mikhailkolesnik opened this issue Jul 2, 2019 · 1 comment
Closed

Regex: Underscore (_) makes replace group not found #32885

mikhailkolesnik opened this issue Jul 2, 2019 · 1 comment

Comments

@mikhailkolesnik
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.11.4 darwin/amd64

Does this issue reproduce with the latest release?

Assuming playground uses it - yes: https://play.golang.org/p/THIwTK5zXpW

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/...Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/.../.../gocode"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.4/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.4/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/th/6k5d1zsx18z5dnzlrsrskxgc0000gp/T/go-build913576916=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://play.golang.org/p/THIwTK5zXpW

package main

import (
	"fmt"
	"regexp"
)

func main() {
	r := regexp.MustCompile(`(\w+)`)
	fmt.Println(r)

	orig := "somerandomtext"
	fmt.Println(r.MatchString(orig))

	fmt.Println(r.ReplaceAllString(orig, "$1"))
	fmt.Println(r.ReplaceAllString(orig, "$1_"))
	fmt.Println(r.ReplaceAllString(orig, "$1 _"))
}

What did you expect to see?

second output should produce somerandomtext_ but produces nothing

(\w+)
true
somerandomtext
somerandomtext_
somerandomtext _

What did you see instead?

(\w+)
true
somerandomtext

somerandomtext _

RE2 issue: google/re2#209

@ianlancetaylor
Copy link
Contributor

The docs for regexp.ReplaceAllString say that it interprets $ as in Expand. The docs for Expand say "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." The names are for use with the (?P<name>re) syntax. So when you write $1_, ReplaceAllString is substituting the value of the variable 1_, which is empty. You need to write ${1}_.

@golang golang locked and limited conversation to collaborators Jul 1, 2020
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