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
相關用法
- GO Regexp.FindAllSubmatch用法及代碼示例
- GO Regexp.FindAllString用法及代碼示例
- GO Regexp.FindAllStringSubmatch用法及代碼示例
- GO Regexp.FindAllStringSubmatchIndex用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
- GO Regexp.FindAll用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Regexp.FindStringSubmatch用法及代碼示例
- GO Regexp.FindStringIndex用法及代碼示例
- GO Regexp.Find用法及代碼示例
- GO Regexp.FindSubmatchIndex用法及代碼示例
- GO Regexp.FindIndex用法及代碼示例
- GO Regexp.FindSubmatch用法及代碼示例
- GO Regexp.ReplaceAllLiteralString用法及代碼示例
- GO Regexp.ExpandString用法及代碼示例
- GO Regexp.SubexpIndex用法及代碼示例
- GO Regexp.Match用法及代碼示例
- GO Regexp.Longest用法及代碼示例
- GO Regexp.ReplaceAll用法及代碼示例
- GO Regexp.Expand用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Regexp.FindAllSubmatchIndex。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。