Search code examples
regexgolooker-studiore2

Exclude pattern in RegExp match Golang


I need to extract a part of a string in Golang for a dashboard in Google Data Studio. This is the string:

ABC - What I need::Other Information I do not need

To get the part between the hyphen and the first colon I have tried ([\\-].*[\\:]), which includes both, the hyphen and the colons.

That might be an easy question for more experienced RegExp users, but how do I match only the words in between?


Solution

  • You may use this:

    -(.*?):
    

    Here the first capture group is your desired result. Example

    Sample Source: ( run here )

    package main
    
    import (
        "fmt"
        "regexp"
    )
    
    func main() {
        var re = regexp.MustCompile(`(?m)-(.*?):`)
        var str = `ABC - What I need::Other Information I do not need`
        rs:=re.FindStringSubmatch(str)
        fmt.Println(rs[1])
    
    }