我有以下文本文件,我想解析出来以获取各个字段:
host_group_web = ( )
host_group_lbnorth = ( lba050 lbhou002 lblon003 )
我要提取的字段以粗体显示
- host_group_web = ( )
- host_group_lbnorth = ( lba505 lbhou002 lblon003 )
host_group_web 在( )之间没有任何项目,因此该部分将被忽略
我将第一个组命名为nodegroup并将 () 之间的项目命名为节点
我正在逐行读取文件,并存储结果以供进一步处理。
在 Golang 中,这是我正在使用的正则表达式的片段:
hostGroupLine := "host_group_lbnorth = ( lba050 lbhou002 lblon003 )"
hostGroupExp := regexp.MustCompile(`host_group_(?P<nodegroup>[[:alnum:]]+)\s*=\s*\(\s*(?P<nodes>[[:alnum:]]+\s*)`)
hostGroupMatch := hostGroupExp.FindStringSubmatch(hostGroupLine)
for i, name := range hostGroupExp.SubexpNames() {
if i != 0 {
fmt.Println("GroupName:", name, "GroupMatch:", hostGroupMatch[i])
}
}
我得到以下输出,其中缺少名为 group的节点的其余匹配项。
GroupName: nodegroup GroupMatch: lbnorth
GroupName: nodes GroupMatch: lba050
我的问题是,如何在 Golang 中获得一个匹配节点组和所有可能在行中的节点的正则表达式,例如 lba050 lbhou002 lblon003。节点的数量会有所不同,从 0 到尽可能多。