[英]Golang regexp issue with FindStringSubmatch
I was trying to use regexp to do some pattern matching with the or operator but I got some odd results. 我试图使用regexp与or运算符进行某种模式匹配,但结果有些奇怪。 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 网址: 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. matchs数组的长度为3,而我认为它的长度应该为2。最后一个值是一个空字符串。 I dont understand why it does this. 我不明白为什么这样做。 Is this a golang bug? 这是golang的错误吗? How do I submit golang bugs? 如何提交golang错误?
The last empty value corresponds to (.+)
and just an indication that this capturing group was not 'hit' while matching. 最后一个空值对应于(.+)
,仅表示此捕获组在匹配时未“命中”。 In other words, it is completely legitimate. 换句话说,这是完全合法的。 In your case it will be safe to use non-capturing group instead: (?:.+)
- http://play.golang.org/p/MEkkoGqxho 在您的情况下,可以使用非捕获组代替: (?:.+)
-http (?:.+)
//play.golang.org/p/MEkkoGqxho
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.