GO語言"regexp"包中"Regexp.SubexpNames"類型的用法及代碼示例。
用法:
func(re *Regexp) SubexpNames() []string
SubexpNames 返回此 Regexp 中帶括號的子表達式的名稱。第一個 sub-expression 的名稱是 names[1],因此如果 m 是匹配切片,則 m[i] 的名稱是 SubexpNames()[i]。由於無法命名整個 Regexp,因此 names[0] 始終為空字符串。不應修改切片。
例子:
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)`)
fmt.Println(re.MatchString("Alan Turing"))
fmt.Printf("%q\n", re.SubexpNames())
reversed := fmt.Sprintf("${%s} ${%s}", re.SubexpNames()[2], re.SubexpNames()[1])
fmt.Println(reversed)
fmt.Println(re.ReplaceAllString("Alan Turing", reversed))
}
輸出:
true ["" "first" "last"] ${last} ${first} Turing Alan
相關用法
- GO Regexp.SubexpIndex用法及代碼示例
- GO Regexp.Split用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
- GO Regexp.ReplaceAllLiteralString用法及代碼示例
- GO Regexp.FindStringSubmatch用法及代碼示例
- GO Regexp.FindAllString用法及代碼示例
- GO Regexp.ExpandString用法及代碼示例
- GO Regexp.FindAllStringSubmatch用法及代碼示例
- GO Regexp.Match用法及代碼示例
- GO Regexp.Longest用法及代碼示例
- GO Regexp.FindStringIndex用法及代碼示例
- GO Regexp.ReplaceAll用法及代碼示例
- GO Regexp.Expand用法及代碼示例
- GO Regexp.Find用法及代碼示例
- GO Regexp.FindAllSubmatchIndex用法及代碼示例
- GO Regexp.FindSubmatchIndex用法及代碼示例
- GO Regexp.FindAllStringSubmatchIndex用法及代碼示例
- GO Regexp.MatchString用法及代碼示例
- GO Regexp.ReplaceAllString用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Regexp.SubexpNames。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。