当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


GO Regexp.FindAllSubmatchIndex用法及代码示例

GO语言"regexp"包中"Regexp.FindAllSubmatchIndex"类型的用法及代码示例。

用法:

func(re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int

FindAllSubmatchIndex 是 FindSubmatchIndex 的'All' 版本;它返回表达式的所有连续匹配的切片,如包注释中的'All' 说明所定义。返回值 nil 表示不匹配。

例子:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    content := []byte(`
    # comment line
    option1: value1
    option2: value2
`)
    // Regex pattern captures "key: value" pair from the content.
    pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`)
    allIndexes := pattern.FindAllSubmatchIndex(content, -1)
    for _, loc := range allIndexes {
        fmt.Println(loc)
        fmt.Println(string(content[loc[0]:loc[1]]))
        fmt.Println(string(content[loc[2]:loc[3]]))
        fmt.Println(string(content[loc[4]:loc[5]]))
    }
}

输出:

[18 33 18 25 27 33]
option1: value1
option1
value1
[35 50 35 42 44 50]
option2: value2
option2
value2

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Regexp.FindAllSubmatchIndex。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。