【问题标题】:Exclude pattern in RegExp match Golang [duplicate]RegExp匹配Golang中的排除模式[重复]
【发布时间】:2020-02-19 10:39:33
【问题描述】:

我需要在 Golang 中提取字符串的一部分以用于 Google Data Studio 中的仪表板。这是字符串:

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

为了得到连字符和第一个冒号之间的部分,我尝试了([\\-].*[\\:]),其中包括连字符和冒号。

对于更有经验的 RegExp 用户来说,这可能是一个简单的问题,但我如何只匹配中间的单词?

【问题讨论】:

  • 我知道这个页面并在那里尽力而为。但我没有找到合适的锚。

标签: regex go google-data-studio re2


【解决方案1】:

你可以使用这个:

-(.*?):

这里的第一个捕获组是您想要的结果。 Example

样本来源:(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])

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多