0

my strings:

12345:cluster/ecs52v-usw2-tst",
12345:cluster/test32-euw1-stg",

The output I'm looking for is:
ecs52v-usw2-tst
test32-euw1-stg

I have multiple cluster names that I'm trying to capture in a slice I've gotten it in ruby (?<=\/)(.*(tst|stg|prd))(?=") but I'm having trouble in golang. https://go.dev/play/p/DyYr3igu2CF

elzwhere
  • 869
  • 1
  • 6
  • 13
  • Are all lines formatted like this? Then you don't need a regex, but can for example use a Scanner to read line-per-line, split on slash, and ignore the last character. – Ferdy Pruis Oct 11 '22 at 05:53

2 Answers2

0

You could use FindAllStringSubmatch to find all submatches.

func main() {
    var re = regexp.MustCompile(`(?mi)(/(.*?)")`)
    var str = `cluster/ecs52v-usw2-tst",
cluster/ecs52v-usw2-stg",
cluster/ecs52v-usw2-prd",`

    matches := re.FindAllStringSubmatch(str, -1)
    for _, match := range matches {
        fmt.Printf("val = %s \n", match[2])
    }
}

Output

val = ecs52v-usw2-tst 
val = ecs52v-usw2-stg 
val = ecs52v-usw2-prd 

https://go.dev/play/p/_LRVYaM2r7z

zangw
  • 43,869
  • 19
  • 177
  • 214
-1
package main

import (
    "fmt"
    "regexp"
)

func main() {
    var re = regexp.MustCompile(`(?mi)/(.*?)"`)
    var str = `cluster/ecs52v-usw2-tst",
cluster/ecs52v-usw2-stg",
cluster/ecs52v-usw2-prd",`

    for i, match := range re.FindAllStringSubmatch(str, -1) {
        fmt.Println(match[1], "found at line", i)
    }
}
Amin Cheloh
  • 465
  • 7
  • 14