在 JSON 字段中查找逗号的正则表达式:

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

Regex for finding commas in a field of a JSON

问题

我想创建一个正则表达式,以帮助我找到位于字符串“"RecruiterEmails"”和“"SourcerEmails"”之间且位于双引号之间的逗号。正则表达式将找到逗号,然后我将使用替换策略将其替换为“\n”。

此JSON作为输入

[
  {
    "JobRefId": "Ref1",
    "RecruiterEmails": "john@gmail.com,carl@gmail.com,mario@gmail.com",
    "SourcerEmails": "robert@gmail.com,jim@gmail.com",
    "JobStatus": "On,Now"
  }
]

应在将选定的逗号替换为“\n”后具有以下输出

[
  {
    "JobRefId": "Ref1",
    "RecruiterEmails": "john@gmail.com\ncarl@gmail.com\nmario@gmail.com",
    "SourcerEmails": "robert@gmail.com,jim@gmail.com",
    "JobStatus": "On,Now"
  }
]

请注意,我不想要行末的逗号(分隔字段“RecruiterEmails”和“SourcerEmails”的逗号),也不想要分隔“SourcerEmails”中的电子邮件的逗号。我只想要分隔“RecruiterEmails”中的电子邮件的逗号。

到目前为止,我已能够起草的正则表达式捕获了“RecruiterEmails”和“SourcerEmails”之间的所有字符,但我只想要指定的逗号。

这是正则表达式:

"RecruiterEmails"([\S\s]*?)"SourcerEmails"

我知道可以通过JOLT转换来完成这个任务,但我使用的软件不支持JOLT转换。唯一可能的方法是通过正则表达式来实现。

非常感谢您提前的帮助!

英文:

I want to create a regex that will help me find the commas that between the strings "RecruiterEmails" and "SourcerEmails" and that are between double quotes. The regex will find the comma and I will use the replace strategy to replace it with \n

This JSON as input

[
  {
    "JobRefId": "Ref1",
    "RecruiterEmails": "john@gmail.com,carl@gmail.com,mario@gmail.com",
    "SourcerEmails": "robert@gmail.com,jim@gmail.com",
    "JobStatus": "On,Now"
  }
]

Should have this output after replacing the selected commas in RecruiterEmails with \n

[
  {
    "JobRefId": "Ref1",
    "RecruiterEmails": "john@gmail.com\ncarl@gmail.com\nmario@gmail.com",
    "SourcerEmails": "robert@gmail.com,jim@gmail.com",
    "JobStatus": "On,Now"
  }
]

Please note that I do NOT want the comma that is end of the line (the one that separates the fields RecruiterEmails and SourcerEmails) and that I do NOT want the commas that separate the emails of SourcerEmails. All I want are the commas that separate the emails of RecruiterEmails.

The regex I have been able to draft so far captures all the characters in between RecruiterEmails and SourcerEmails, but I want only the specified commas.

This is the regex:

"RecruiterEmails"([\S\s]*?)"SourcerEmails"

I know that this can be done through a JOLT transformation, but the software I am using does not support JOLT transformations. The only possible way is to do it through a regex.

Thank you in advance!

答案1

得分: 1

你可以使用前瞻来断定你正在处理的是在正则表达式中的"SourcerEmails"之前的那一行:

,(?=.*",\s+"SourcerEmails")
英文:

You can use look ahead to assert that you are on the line that precedes "SourcerEmails" with this regex:

,(?=.*",\s+"SourcerEmails")

huangapple
  • 本文由 发表于 2023年2月10日 03:15:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403401.html
匿名

发表评论

匿名网友

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

确定