當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO Regexp.FindSubmatchIndex用法及代碼示例

GO語言"regexp"包中"Regexp.FindSubmatchIndex"類型的用法及代碼示例。

用法:

func(re *Regexp) FindSubmatchIndex(b []byte) []int

FindSubmatchIndex 返回一個切片,其中包含標識 b 中正則表達式的最左側匹配及其子表達式的匹配(如果有)的索引對,如包注釋中的 'Submatch' 和 'Index' 說明所定義。返回值 nil 表示不匹配。

例子:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`a(x*)b`)
	// Indices:
	//    01234567   012345678
	//    -ab-axb-   -axxb-ab-
	fmt.Println(re.FindSubmatchIndex([]byte("-ab-")))
	fmt.Println(re.FindSubmatchIndex([]byte("-axxb-")))
	fmt.Println(re.FindSubmatchIndex([]byte("-ab-axb-")))
	fmt.Println(re.FindSubmatchIndex([]byte("-axxb-ab-")))
	fmt.Println(re.FindSubmatchIndex([]byte("-foo-")))
}

輸出:

[1 3 2 2]
[1 5 2 4]
[1 3 2 2]
[1 5 2 4]
[]

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Regexp.FindSubmatchIndex。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。