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: bug in submatch functions? #11899

Closed
ghbutton opened this issue Jul 28, 2015 · 1 comment
Closed

regexp: bug in submatch functions? #11899

ghbutton opened this issue Jul 28, 2015 · 1 comment

Comments

@ghbutton
Copy link

I was trying to use regexp to do some pattern matching with the or operator but I got some odd results. I have stripped out everything but the essentials to show the problem with my result.

This is my code:

package main

import "fmt"
import "regexp"

func main() {
    authRegexp := regexp.MustCompile("^token=(llll|(.+))$")
    matches := authRegexp.FindStringSubmatch("token=llll")
    fmt.Println("MATCHES", matches, len(matches))
        // MATCHES [token=llll llll ] 3
}

Url: http://play.golang.org/p/nLtWQQgveY

The matches array has a length of 3, when I think it should have a length of 2. The last value is an empty string. I dont understand why it does this. Is this a golang bug? How do I submit golang bugs?

@adg adg changed the title Golang Regexp bug? regexp: bug in submatch functions? Jul 28, 2015
@adg
Copy link
Contributor

adg commented Jul 28, 2015

It's not a bug. You have two submatches, and the entire expression is returned as submatch 0. That's three. Here's the relevant portion of the docs:

If 'Submatch' is present, the return value is a slice identifying the successive submatches of the expression. Submatches are matches of parenthesized subexpressions (also known as capturing groups) within the regular expression, numbered from left to right in order of opening parenthesis. Submatch 0 is the match of the entire expression, submatch 1 the match of the first parenthesized subexpression, and so on.

@adg adg closed this as completed Jul 28, 2015
@golang golang locked and limited conversation to collaborators Aug 5, 2016
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