在golang中删除正则表达式的第一个匹配项。

huangapple go评论83阅读模式
英文:

Remove first occurence of match in regex golang

问题

我有以下字符串:

new k8s.KubeRoleBinding(this, "argocd-application-controller", {
    kind: "RoleBinding",
    metadata: {
        labels: {
            "app.kubernetes.io/component": "application-controller",
            "app.kubernetes.io/name": "argocd-application-controller",
            "app.kubernetes.io/part-of": "argocd",
        },
        name: "argocd-application-controller",
    },
    roleRef: {
        apiGroup: "rbac.authorization.k8s.io",
        kind: "Role",
        name: "argocd-application-controller",
    },
    subjects: [{
        kind: "ServiceAccount",
        name: "argocd-application-controller",
    }],
});

我想删除第一个出现的 kind: 的行。

到目前为止,我尝试了以下方法,但它会删除所有出现的行。

re := regexp.MustCompile(`(?m)[\r\n]+^.*kind.*$`)
res := re.ReplaceAllString(str, "")

希望得到的字符串是:

new k8s.KubeRoleBinding(this, "argocd-application-controller", {
    metadata: {
        labels: {
            "app.kubernetes.io/component": "application-controller",
            "app.kubernetes.io/name": "argocd-application-controller",
            "app.kubernetes.io/part-of": "argocd",
        },
        name: "argocd-application-controller",
    },
    roleRef: {
        apiGroup: "rbac.authorization.k8s.io",
        name: "argocd-application-controller",
    },
    subjects: [{
        kind: "ServiceAccount",
        name: "argocd-application-controller",
    }],
});
英文:

I have the following string:

new k8s.KubeRoleBinding(this, "argocd-application-controller", {
    kind: "RoleBinding",
    metadata: {
        labels: {
            "app.kubernetes.io/component": "application-controller",
            "app.kubernetes.io/name": "argocd-application-controller",
            "app.kubernetes.io/part-of": "argocd",
        },
        name: "argocd-application-controller",
    },
    roleRef: {
        apiGroup: "rbac.authorization.k8s.io",
        kind: "Role",
        name: "argocd-application-controller",
    },
    subjects: [{
        kind: "ServiceAccount",
        name: "argocd-application-controller",
    }],
});

Id like to remove the line which has the first occurence of kind:

I tried the following so far, but it removes all occurrences.

	re := regexp.MustCompile("(?m)[\r\n]+^.*kind.*$")
	res := re.ReplaceAllString(str, "$1")

Playground link with code : https://play.golang.org/p/SMiyTJvKNVF

Wanted string :

new k8s.KubeRoleBinding(this, "argocd-application-controller", {
    metadata: {
        labels: {
            "app.kubernetes.io/component": "application-controller",
            "app.kubernetes.io/name": "argocd-application-controller",
            "app.kubernetes.io/part-of": "argocd",
        },
        name: "argocd-application-controller",
    },
    roleRef: {
        apiGroup: "rbac.authorization.k8s.io",
        kind: "Role",
        name: "argocd-application-controller",
    },
    subjects: [{
        kind: "ServiceAccount",
        name: "argocd-application-controller",
    }],
});

答案1

得分: 2

找到第一个匹配项的位置。使用字符串切片操作删除匹配项。

loc := re.FindStringIndex(str)
res := str
if loc != nil {
	res = str[:loc[0]] + str[loc[1]:]
}

在 playground 上运行示例

英文:

Find the location of the first match. Use string slice operations to delete the match.

loc := re.FindStringIndex(str)
res := str
if loc != nil {
	res = str[:loc[0]] + str[loc[1]:]
}

Run the example on the playground.

huangapple
  • 本文由 发表于 2021年9月9日 00:04:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/69106370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定