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: Repeated capture groups only capturing final captured match. #43332

Closed
dwedul-figure opened this issue Dec 22, 2020 · 2 comments
Closed

regexp: Repeated capture groups only capturing final captured match. #43332

dwedul-figure opened this issue Dec 22, 2020 · 2 comments

Comments

@dwedul-figure
Copy link

@dwedul-figure dwedul-figure commented Dec 22, 2020

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

$ go version
go version go1.15.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/danielwedul/Library/Caches/go-build"
GOENV="/Users/danielwedul/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/danielwedul/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/danielwedul/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.2/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
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/px/yz_xp7wj195g7n9k2h4dxp8w0000gp/T/go-build197526322=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

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

What did you expect to see?

Regex: [^(A+B+)+$]
input: [ABAABBAAABBB].
FindStringSubmatch - Expected: [ABAABBAAABBB AB AABB AAABBB]
FindStringSubmatch -   Actual: [ABAABBAAABBB AB AABB AAABBB]
FindStringSubmatchIndex - Expected: [0 12 0 2 2 6 6 12]
FindStringSubmatchIndex -   Actual: [0 12 0 2 2 6 6 12]
FindAllStringSubmatch - Expected: [[ABAABBAAABBB AB] [ABAABBAAABBB AABB] [ABAABBAAABBB AAABBB]]
FindAllStringSubmatch -   Actual: [[ABAABBAAABBB AB] [ABAABBAAABBB AABB] [ABAABBAAABBB AAABBB]]
FindAllStringSubmatchIndex - Expected: [[0 12 0 2] [0 12 2 6] [0 12 6 12]]
FindAllStringSubmatchIndex -   Actual: [[0 12 0 2] [0 12 2 6] [0 12 6 12]]

What did you see instead?

Regex: [^(A+B+)+$]
input: [ABAABBAAABBB].
FindStringSubmatch - Expected: [ABAABBAAABBB AB AABB AAABBB]
FindStringSubmatch -   Actual: [ABAABBAAABBB AAABBB]
FindStringSubmatchIndex - Expected: [0 12 0 2 2 6 6 12]
FindStringSubmatchIndex -   Actual: [0 12 6 12]
FindAllStringSubmatch - Expected: [[ABAABBAAABBB AB] [ABAABBAAABBB AABB] [ABAABBAAABBB AAABBB]]
FindAllStringSubmatch -   Actual: [[ABAABBAAABBB AAABBB]]
FindAllStringSubmatchIndex - Expected: [[0 12 0 2] [0 12 2 6] [0 12 6 12]]
FindAllStringSubmatchIndex -   Actual: [[0 12 6 12]]
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Dec 22, 2020

I'm not really sure what to say except that that isn't how it works. The number of subexpressions of a regexp is fixed by the definition of the regexp. It does not depend on the input string.

@dwedul-figure
Copy link
Author

@dwedul-figure dwedul-figure commented Dec 22, 2020

Sorry. I could have sworn that that was how it worked in some other languages. I just tested it in Perl, though, and I am mistaken.

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

Successfully merging a pull request may close this issue.

None yet
2 participants