hookenz
hookenz

Reputation: 38889

How do I capture multiple groups in a complicated golang regexp capture

I'm having a bit of trouble with my regular expression.

Given the following line of text I'm wanting to create a regular expression that captures the dll and TLB filename, the text before the brackets and the version number after the bracket.

Enabler .Net API (ITL.Enabler.Api.dll, ITL.Enabler.Api_pcl.dll, ITL.Enabler.API.TLB) v1.3.2

In fact I want it to capture also *.sys, *.exe as well.

This is what I have so far which matches the dll's.

([\w .]*.dll)

But does not capture both groups. Just the last file is in the group. How do I make it capture all the files?

To capture the version number I'm expecting something like the following. Version is of the form v1.2.n.n The n.n is optional.

(v\d.{2,})

I was hoping to be able to capture in one line *.dll, *.sys, *.tlb etc. I want the extension to be case insensitive.

So something like this does that. ([\w .]*.[d|D][l|L][l|l])

--- update ---

Code:

txt := "Enabler .Net API (ITL.Enabler.Api.dll, ITL.Enabler.Api_pcl.dll, ITL.Enabler.API.TLB) v1.3.2"
re := regexp.MustCompile(`(?i)([\w .]*\.dll)`)
match := re.FindStringSubmatch(txt)

for i := range match {
    fmt.Println(match[i])
}

What I want to capture are all the dll versions as well as any file ending in sys and exe and tlb. I then want the version number.

Not sure how to repeat the capture. It only shows the last dll entry

Upvotes: 1

Views: 1500

Answers (1)

Sarath Sadasivan Pillai
Sarath Sadasivan Pillai

Reputation: 7091

I would suggest two different regex for libraries and versions . Following code does the same on given string

package main

import (
    "fmt"
    "regexp"
)

func main() {

    txt := "Enabler .Net API (ITL.Enabler.Api.dll, ITL.Enabler.Api_pcl.dll, ITL.Enabler.API.TLB) v1.1.3"
    libRE := regexp.MustCompile(`(?i)([\w_.]+\.(dll|sys|exe|tlb))`)
    versionRE := regexp.MustCompile(`v(\d+\.)*\d+`)

    libraries := libRE.FindAllString(txt, -1)
    version := versionRE.FindString(txt)
    for _, lib := range libraries {
        fmt.Println(lib)
    }
    fmt.Println(version)
}

Checkout code here : https://play.golang.org/p/Hh4B23biKE5

Upvotes: 1

Related Questions