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

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

Remove first occurence of match in regex golang

问题

我有以下字符串:

  1. new k8s.KubeRoleBinding(this, "argocd-application-controller", {
  2. kind: "RoleBinding",
  3. metadata: {
  4. labels: {
  5. "app.kubernetes.io/component": "application-controller",
  6. "app.kubernetes.io/name": "argocd-application-controller",
  7. "app.kubernetes.io/part-of": "argocd",
  8. },
  9. name: "argocd-application-controller",
  10. },
  11. roleRef: {
  12. apiGroup: "rbac.authorization.k8s.io",
  13. kind: "Role",
  14. name: "argocd-application-controller",
  15. },
  16. subjects: [{
  17. kind: "ServiceAccount",
  18. name: "argocd-application-controller",
  19. }],
  20. });

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

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

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

希望得到的字符串是:

  1. new k8s.KubeRoleBinding(this, "argocd-application-controller", {
  2. metadata: {
  3. labels: {
  4. "app.kubernetes.io/component": "application-controller",
  5. "app.kubernetes.io/name": "argocd-application-controller",
  6. "app.kubernetes.io/part-of": "argocd",
  7. },
  8. name: "argocd-application-controller",
  9. },
  10. roleRef: {
  11. apiGroup: "rbac.authorization.k8s.io",
  12. name: "argocd-application-controller",
  13. },
  14. subjects: [{
  15. kind: "ServiceAccount",
  16. name: "argocd-application-controller",
  17. }],
  18. });
英文:

I have the following string:

  1. new k8s.KubeRoleBinding(this, "argocd-application-controller", {
  2. kind: "RoleBinding",
  3. metadata: {
  4. labels: {
  5. "app.kubernetes.io/component": "application-controller",
  6. "app.kubernetes.io/name": "argocd-application-controller",
  7. "app.kubernetes.io/part-of": "argocd",
  8. },
  9. name: "argocd-application-controller",
  10. },
  11. roleRef: {
  12. apiGroup: "rbac.authorization.k8s.io",
  13. kind: "Role",
  14. name: "argocd-application-controller",
  15. },
  16. subjects: [{
  17. kind: "ServiceAccount",
  18. name: "argocd-application-controller",
  19. }],
  20. });

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

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

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

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

Wanted string :

  1. new k8s.KubeRoleBinding(this, "argocd-application-controller", {
  2. metadata: {
  3. labels: {
  4. "app.kubernetes.io/component": "application-controller",
  5. "app.kubernetes.io/name": "argocd-application-controller",
  6. "app.kubernetes.io/part-of": "argocd",
  7. },
  8. name: "argocd-application-controller",
  9. },
  10. roleRef: {
  11. apiGroup: "rbac.authorization.k8s.io",
  12. kind: "Role",
  13. name: "argocd-application-controller",
  14. },
  15. subjects: [{
  16. kind: "ServiceAccount",
  17. name: "argocd-application-controller",
  18. }],
  19. });

答案1

得分: 2

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

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

在 playground 上运行示例

英文:

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

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

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:

确定