英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论