In Regexp.FindAllStringSubmatch(), what does the second parameter do?

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

In Regexp.FindAllStringSubmatch(), what does the second parameter do?

问题

第二个参数是用来指定匹配的次数的。在这个例子中,如果将第二个参数设置为1,那么只会匹配到一个"a";如果设置为2,那么会匹配到两个"a";以此类推。你可以参考官方文档或者一些链接来获取确切的答案。

英文:

In this method:

func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string

What does the second parameter do?
I have tried:

re, _ := regexp.Compile("a")
rs := re.FindAllString("aaaaa, ", **1**) // 1 get one 'a', 2 get two 'a's, 3 get three 'a's ...
for _,v := range rs {
	fmt.Println(v)

}

It seems that the second parameter is about how many times it matches. Am I right?
Could anybody give me an answer for sure? Official doc or some links is prefer.

答案1

得分: 12

如果存在'All',该例程将匹配整个表达式的连续非重叠匹配。忽略与前一个匹配相邻的空匹配。返回值是一个包含相应非'All'例程的连续返回值的切片。这些例程接受额外的整数参数n;如果n >= 0,函数最多返回n个匹配/子匹配。

英文:

Citation from the overview section of http://golang.org/pkg/regexp/:

> If 'All' is present, the routine matches successive non-overlapping matches of the entire expression. Empty matches abutting a preceding match are ignored. The return value is a slice containing the successive return values of the corresponding non-'All' routine. These routines take an extra integer argument, n; if n >= 0, the function returns at most n matches/submatches.

huangapple
  • 本文由 发表于 2012年4月23日 23:54:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/10283930.html
匿名

发表评论

匿名网友

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

确定