FindStringSubmatch返回匹配的组两次。

huangapple go评论84阅读模式
英文:

FindStringSubmatch returns matched group twice

问题

也许我对Go语言的regexp.FindStringSubmatch()函数有一些非常基本的东西不了解。我想捕获紧跟在字符串"Serial Number: "后面的所有数字,并获得了意外的输出。我的代码如下:

package main
import "fmt"
import "regexp"

func main() {
    
    x := "Serial Number: 12334"
    r := regexp.MustCompile(`(\d+)`)
    res := r.FindStringSubmatch(x)

    for i,val := range res {
        fmt.Printf("entry %d:%s\n", i,val)
    }
}

输出结果为:

entry 0:12334
entry 1:12334

我更熟悉Python的分组功能,它看起来非常简单:

>>> re.search('(\d+)', "Serial Number: 12344").groups()[0]
'12344'

我该如何在Go语言中实现分组功能呢?谢谢。

英文:

Perhaps I'm missing something very basic about the go's regexp.FindStringSubmatch(). I want to capture the group with all digits that follows the string "Serial Number: " but get unexpected output. My code is as below:

package main
import "fmt"
import "regexp"

func main() {
	
	x := "Serial Number: 12334"
	r := regexp.MustCompile(`(\d+)`)
	res := r.FindStringSubmatch(x)

	for i,val := range res {
		fmt.Printf("entry %d:%s\n", i,val)
	}
}

The output is:

entry 0:12334
entry 1:12334

I'm more familiar with python's grouping that seems pretty straight-forward:

>>> re.search('(\d+)', "Serial Number: 12344").groups()[0]
'12344'

How can I get the grouping to work in go?
thanks

答案1

得分: 6

Regexp.FindStringSubmatch

> FindStringSubmatch返回一个字符串切片,其中包含正则表达式在s中最左边匹配的文本和匹配项。

所以:

  • 第一个条目是已匹配的内容:'12334'(最左边的匹配)
  • 第二个条目是捕获的组:'12334'

另一个例子:

re := regexp.MustCompile("a(x*)b(y|z)c")
fmt.Printf("%q\n", re.FindStringSubmatch("-axxxbyc-"))

这将打印:

  • 最左边的匹配:"axxxbyc"

  • 两个捕获的组:"xxx" "y"

英文:

From Regexp.FindStringSubmatch:

> FindStringSubmatch returns a slice of strings holding the text of the leftmost match of the regular expression in s and the matches

So:

  • the first entry is what has been matched: '12334' (leftmost match)
  • the second entry is the one captured group: '12334'

Another example:

re := regexp.MustCompile("a(x*)b(y|z)c")
fmt.Printf("%q\n", re.FindStringSubmatch("-axxxbyc-"))

That would print:

  • the leftmost match: "axxxbyc"

  • the two captured groups: "xxx" "y"

huangapple
  • 本文由 发表于 2014年11月15日 02:17:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/26936396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定