Skip to content
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

regexp: i used "(?:re)" regex but found maybe it's capturing-group but as describe in package #21185

Closed
yingqin678 opened this issue Jul 27, 2017 · 4 comments
Milestone

Comments

@yingqin678
Copy link

@yingqin678 yingqin678 commented Jul 27, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.8.3 windows/amd64

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

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\isource\go-workspace
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
set PKG_CONFIG=pkg-config
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2

What did you do?

Ihttps://play.golang.org/p/AgklWl8CWw

What did you expect to see?

i want it come out to be "1234", as "|" is not capturing

What did you see instead?

"1234|"

@rakyll rakyll added this to the Go1.10 milestone Jul 27, 2017
@bradleyfalzon
Copy link

@bradleyfalzon bradleyfalzon commented Jul 27, 2017

Is this a case of needing to use a raw string instead of string literal as it appears the backslash is being escaped?

regexp.Compile(`^1234(?:\\|)`)

Instead of:

regexp.Compile("^1234(?:\\|)")
@yingqin678
Copy link
Author

@yingqin678 yingqin678 commented Jul 27, 2017

@bradleyfalzon yeah,it's effective;but why when i code like this "regexp.Compile(^123(?:4))", it come out "1234" ?https://play.golang.org/p/jB7FmxWz9r**

@bradleyfalzon
Copy link

@bradleyfalzon bradleyfalzon commented Jul 27, 2017

In quoted string literal a \ is used as a part of a escape sequence, a \\ escapes this. See https://golang.org/ref/spec#String_literals (and https://golang.org/ref/spec#Rune_literals)

To see this behaviour:

fmt.Println(`^1234(?:\\|)`) // outputs: ^1234(?:\\|)
fmt.Println("^1234(?:\\|)") // outputs: ^1234(?:\|)

You wanted the first behaviour, and therefore should use raw string.

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Jul 27, 2017

For questions about Go, see https://golang.org/wiki/Questions.

@bradfitz bradfitz closed this Jul 27, 2017
@mikioh mikioh changed the title i used "(?:re)" regex but found maybe it's capturing-group but as describe in package regexp: i used "(?:re)" regex but found maybe it's capturing-group but as describe in package Aug 2, 2017
@golang golang locked and limited conversation to collaborators Aug 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.