用正则表达式替换文本。

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

Jolt replace text using regex

问题

以下是翻译好的部分:

Input

  1. [
  2. {
  3. "name": "abc_google@gmail.com"
  4. },
  5. {
  6. "name": "ayan.agrawal$1990@gmail.com"
  7. }
  8. ]

Regex to be used

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

Expected output:

  1. [
  2. {
  3. "name": "abcgoogle@gmailcom"
  4. },
  5. {
  6. "name": "ayanagrawal$1990@gmailcom"
  7. }
  8. ]
英文:

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

  1. [
  2. {
  3. "name": "abc_google@gmail.com"
  4. },
  5. {
  6. "name": "ayan.agrawal$1990@gmail.com"
  7. }
  8. ]

Regex to be used

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

Expected output:

  1. [
  2. {
  3. "name": "abcgoogle@gmailcom"
  4. },
  5. {
  6. "name": "ayanagrawal$1990@gmailcom"
  7. }
  8. ]

答案1

得分: 1

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

  1. [
  2. {
  3. "operation": "modify-overwrite-beta",
  4. "spec": {
  5. "*": {
  6. "nm0": "=split('.', @(1, name))",
  7. "nm1": "=join('', @(1, nm0))",
  8. "nm2": "=split('_', @(1, nm1))",
  9. "name": "=join('', @(1, nm2))"
  10. }
  11. }
  12. },
  13. {
  14. "operation": "remove",
  15. "spec": {
  16. "*": {
  17. "nm*": ""
  18. }
  19. }
  20. }
  21. ]
英文:

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

  1. [
  2. {
  3. "operation": "modify-overwrite-beta",
  4. "spec": {
  5. "*": {
  6. "nm0": "=split('\\.',@(1,name))",
  7. "nm1": "=join('',@(1,nm0))",
  8. "nm2": "=split('_',@(1,nm1))",
  9. "name": "=join('',@(1,nm2))"
  10. }
  11. }
  12. },
  13. {
  14. "operation": "remove",
  15. "spec": {
  16. "*": {
  17. "nm*": ""
  18. }
  19. }
  20. }
  21. ]

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:

确定