用正则表达式替换文本。

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

Jolt replace text using regex

问题

以下是翻译好的部分:

Input

[
  {
    "name": "abc_google@gmail.com"
  },
  {
    "name": "ayan.agrawal$1990@gmail.com"
  }
]

Regex to be used

[^a-zA-Z0-9#$@]

Expected output:

[
  {
    "name": "abcgoogle@gmailcom"
  },
  {
    "name": "ayanagrawal$1990@gmailcom"
  }
]
英文:

I am trying to transform an input data where I need to transform/replace the text on the basis of a regex. Not able to find any examples on how this can be done in Jolt spec. Could someone please help on this. Thanks!

Input

[
  {
    "name": "abc_google@gmail.com"
  },
  {
    "name": "ayan.agrawal$1990@gmail.com"
  }
]

Regex to be used

[^a-zA-Z0-9#$@]

Expected output:

[
  {
    "name": "abcgoogle@gmailcom"
  },
  {
    "name": "ayanagrawal$1990@gmailcom"
  }
]

答案1

得分: 1

你不能使用正则表达式来完成这种方式。需要反其道而行之,例如,逐个渲染要移除的每个字符,然后可以通过连续应用splitjoin函数来实现,如下所示:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "nm0": "=split('.', @(1, name))",
        "nm1": "=join('', @(1, nm0))",
        "nm2": "=split('_', @(1, nm1))",
        "name": "=join('', @(1, nm2))"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "nm*": ""
      }
    }
  }
]
英文:

You cannot do in the way of doing with a regex. Need to think on the contrary manner, eg. rendering individually by each character to be removed, and we can do it by successively applying split and join functions such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "nm0": "=split('\\.',@(1,name))",
        "nm1": "=join('',@(1,nm0))",
        "nm2": "=split('_',@(1,nm1))",
        "name": "=join('',@(1,nm2))"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "nm*": ""
      }
    }
  }
]

huangapple
  • 本文由 发表于 2023年7月6日 20:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629081.html
匿名

发表评论

匿名网友

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

确定