Golang: extract data with Regex

后端 未结 4 933
萌比男神i
萌比男神i 2021-02-12 16:00

I\'m trying to extract whatever data inside ${}.

For example, the data extracted from this string should be abc.

git commit -m          


        
4条回答
  •  礼貌的吻别
    2021-02-12 16:28

    You can try with this too,

    re := regexp.MustCompile("\\$\\{(.*?)\\}")
    
    str := "git commit -m '${abc}'"
    res := re.FindAllStringSubmatch(str, 1)
    for i := range res {
        //like Java: match.group(1)
        fmt.Println("Message :", res[i][1])
    }
    

    GoPlay: https://play.golang.org/p/PFH2oDzNIEi

提交回复
热议问题