猿问

Golang 正则表达式命名组和子匹配

我正在尝试匹配正则表达式并获取匹配的捕获组名称。这在正则表达式只匹配字符串一次时有效,但如果匹配字符串不止一次,SubexpNames则不会返回重复的名称。


下面是一个例子:


package main


import (

    "fmt"

    "regexp"

)


func main() {

    re := regexp.MustCompile("(?P<first>[a-zA-Z]+) ")

    fmt.Printf("%q\n", re.SubexpNames())

    fmt.Printf("%q\n", re.FindAllStringSubmatch("Alan Turing ", -1))

}

输出是:


["" "first"]

[["Alan " "Alan"] ["Turing " "Turing"]]

是否可以获得每个子匹配的捕获组名称?


富国沪深
浏览 245回答 2
2回答

九州编程

组名和位置是固定的:re := regexp.MustCompile("(?P<first>[a-zA-Z]+) ")groupNames := re.SubexpNames()for matchNum, match := range re.FindAllStringSubmatch("Alan Turing ", -1) {&nbsp; &nbsp; for groupIdx, group := range match {&nbsp; &nbsp; &nbsp; &nbsp; name := groupNames[groupIdx]&nbsp; &nbsp; &nbsp; &nbsp; if name == "" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = "*"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("#%d text: '%s', group: '%s'\n", matchNum, group, name)&nbsp; &nbsp; }}

素胚勾勒不出你

这可能包含在 Go 1.14(2020 年第一季度,尚未确认)中。参见“提案:正则表达式:添加(*Regexp).SubexpIndex#32420 ”。更新:它已包含在 Go 1.15(2020 年 8 月)的commit 782fcb4中。// SubexpIndex returns the index of the first subexpression with the given name,// or else -1 if there is no subexpression with that name.//// Note that multiple subexpressions can be written using the same name, as in// (?P<bob>a+)(?P<bob>b+), which declares two subexpressions named "bob".// In this case SubexpIndex returns the index of the leftmost such subexpression// in the regular expression.func (*Regexp) SubexpIndex(name string) int这在CL 187919 中进行了讨论。re := regexp.MustCompile(`(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)`)fmt.Println(re.MatchString("Alan Turing"))matches := re.FindStringSubmatch("Alan Turing")lastIndex := re.SubexpIndex("last")fmt.Printf("last => %d\n", lastIndex)fmt.Println(matches[lastIndex])// Output:// true// last => 2// Turing
随时随地看视频慕课网APP

相关分类

Go
我要回答