How to capture 'multiple' repeated groups with Regular Expressions
Asked Answered
A

1

6

I have the following text file I would like to parse out to get the individual fields:

host_group_web = ( )
host_group_lbnorth = ( lba050 lbhou002 lblon003 )

The fields that I would like to extract are in bold

  • host_group_web = ( )
  • host_group_lbnorth = ( lba505 lbhou002 lblon003 )

host_group_web has no items in between the ( ), so that portion would be ignored

I've named the first group as nodegroup and the items in between the () as nodes

I am reading the file line by line, and storing the results for further processing.

In Golang, This is the snippet of Regex I am using:

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])
  }
}

I get the following output, which is missing the rest of the matches for the nodes named group.

GroupName: nodegroup GroupMatch: lbnorth
GroupName: nodes GroupMatch: lba050

The Snippet in Golang Playground

My question is, how do I get a Regex in Golang that would match the nodegroup and all the nodes that maybe in the line, e.g lba050 lbhou002 lblon003. The amount of nodes will vary, from 0 - as many.

Abirritant answered 5/11, 2016 at 6:0 Comment(2)
You can repeadetly match a capturing group, but it will only store the last match for that group. I'd say there is no way around first capturing all the nodes in one turn, then split them in a second turn.Bozcaada
Thanks. I guess that will save me some brain wracking. Cheers!Abirritant
H
5

If you want to capture the group name and all possible node names, you should work with a different regex pattern. This one should capture all of them in one go. No need to work with named capture groups but you can if you want to.

hostGroupExp := regexp.MustCompile(`host_group_([[:alnum:]]+)|([[:alnum:]]+) `)

hostGroupLine := "host_group_lbnorth = ( lba050 lbhou002 lblon003 )"
hostGroupMatch := hostGroupExp.FindAllStringSubmatch(hostGroupLine, -1)

fmt.Printf("GroupName: %s\n", hostGroupMatch[0][1])
for i := 1; i < len(hostGroupMatch); i++ {
    fmt.Printf("  Node: %s\n", hostGroupMatch[i][2])
}

See it in action in playground

Alternative:

You can also work the way awk would do the parsing: use a regexp expression to split the lines in tokens and print the tokens you need. Of course the line layout should be the same as the one given in your example.

package main

import (
    "fmt"
    "regexp"
)

func printGroupName(tokens []string) {
    fmt.Printf("GroupName: %s\n", tokens[2])
    for i := 5; i < len(tokens)-1; i++ {
        fmt.Printf("  Node: %s\n", tokens[i])
    }
}

func main() {

    // regexp line splitter (either _ or space)
    r := regexp.MustCompile(`_| `)

    // lines to parse
    hostGroupLines := []string{
        "host_group_lbnorth = ( lba050 lbhou002 lblon003 )",
        "host_group_web = ( web44 web125 )",
        "host_group_web = ( web44 )",
        "host_group_lbnorth = ( )",
    }

    // split lines on regexp splitter and print result
    for _, line := range hostGroupLines {
        hostGroupMatch := r.Split(line, -1)
        printGroupName(hostGroupMatch)
    }

}

See it in action in playground

Hazard answered 5/11, 2016 at 19:18 Comment(1)
You are correct. Changing the regex is actually the better solution. Thank you for this code. I will sure keep it for future reference.Abirritant

© 2022 - 2024 — McMap. All rights reserved.