解析文本文件以匹配字符串并提取值(在Golang中)

I am scratching my head around a need that seems to be potentially common but I couldn't locate any example on the web.

I have a file like this:

  answer VNET_1_DHCP yes
  answer VNET_1_DHCP_CFG_HASH 4CF2C196E368CE83B9D1895C5E05301CDFDEBCA0  
  answer VNET_1_HOSTONLY_NETMASK 255.255.255.0
  answer VNET_1_HOSTONLY_SUBNET 192.168.224.0
  answer VNET_1_VIRTUAL_ADAPTER yes
  answer VNET_8_DHCP yes
  answer VNET_8_DHCP_CFG_HASH D326C0BC7FF6C38C57AF341F9075E576C175B250
  answer VNET_8_HOSTONLY_NETMASK 255.255.255.0
  answer VNET_8_HOSTONLY_SUBNET 172.16.102.0
  answer VNET_8_NAT yes
  answer VNET_8_VIRTUAL_ADAPTER yes

I need to extract the VNET number of a specific subnet (192.168.224.0). VNET numbers could vary (and the subnet could, in theory, not even exist). So I need to match if the subnet exists, and if exists extract the network number (1 in this example).

I found it to be SUPER easy to implement this in BASH:

 if grep -q 192.168.224.0 ./networking; then
      echo "The ${VMNET_SUBNET} network already exists"
      NETWORK_NUMBER=$(grep ${VMNET_SUBNET} ./networking | cut -d'_' -f 2)
      echo NETWORK_NUMBER  
 else <do something to create it.....> 

I am trying to find the easiest way to implement this using Go.

Thanks.

You can use a regular expression:

re := regexp.MustCompile(`.*VNET_(\d+)_.*192.168.224.0`)
matches := re.FindStringSubmatch(text)
fmt.Println(matches[1])

Playground: http://play.golang.org/p/NQlA2BObtU.

Here is a robust way to parse these data that builds on @ainar-g's answer:

http://play.golang.org/p/6-PELcLvVz

The goal here is to store the properties for each VNET in a map, using the following type:

type vnet map[int]map[string]string

This code:

var re = regexp.MustCompile(`.*VNET_(\d+)_([^\s]+) (.*)`)
func ReadVnet(r io.Reader) vnet {
    s := bufio.NewScanner(r)
    v := make(vnet)

    for s.Scan() {
        matches := re.FindStringSubmatch(s.Text())
        id, err := strconv.Atoi(matches[1])
        if err != nil {
            continue
        }

        if _, ok := v[id]; !ok {
            v[id] = make(map[string]string)
        }
        v[id][matches[2]] = matches[3]
    }

    return v
}

creates the map in question:

map[1:map[DHCP:yes DHCP_CFG_HASH:4CF2C196E368CE83B9D1895C5E05301CDFDEBCA0 HOSTONLY_NETMASK:255.255.255.0 HOSTONLY_SUBNET:192.168.224.0 VIRTUAL_ADAPTER:yes] 8:map[DHCP:yes DHCP_CFG_HASH:D326C0BC7FF6C38C57AF341F9075E576C175B250 HOSTONLY_NETMASK:255.255.255.0 HOSTONLY_SUBNET:172.16.102.0 NAT:yes VIRTUAL_ADAPTER:yes]]

Now you can iterate on the map to find the item of interest:

func main() {
    v := ReadVnet(bytes.NewBufferString(text))

    for id, properties := range v {
        if ip, ok := properties["HOSTONLY_SUBNET"]; ok && ip == "192.168.224.0" {
            fmt.Println(id)
            return
        }
    }
}

This is a version without regexp:

  idxEnd := strings.Index(txt, "192.168.224.0")
  idxVNET := strings.LastIndex(txt[:idxEnd], "VNET_")
  beginNumber := idxVNET + 5
  length := strings.Index(txt[beginNumber:idxEnd], "_")
  number, _ := strconv.Atoi(txt[beginNumber : beginNumber+length])
  fmt.Printf("number: %T %v
", number, number)

Would you try to do that on a very (very) big string, it should be faster.