正则表达式:从Json值中删除双引号

时间:2022-09-15 13:07:00

I am absolutly awful at regex and I now have a problem I can only fix by regex

我对正则表达式绝对可怕,我现在有一个问题,我只能用正则表达式修复

I have a json string that has double quotes in the wrong places

我有一个json字符串,在错误的地方有双引号

for example:

[
  {
    "customerid": "1234",
    "price": "123.456",
    "foo","bar"
  }
]

I would like to remove the double quotes from all the fields that are numbers (customerid, price) and keep them for any alpha numeric characters(foo)

我想删除所有字段中的双引号(customerid,price)并保留它们以用于任何字母数字字符(foo)

Is there a magic regex that can help me achieve this?

是否有一个神奇的正则表达式可以帮助我实现这一目标?

2 个解决方案

#1


0  

To remove double quotes around "simple" numbers use the following regex pattern:

要删除“简单”数字周围的双引号,请使用以下正则表达式模式:

"(-?\d+(\.\d+)?)"

substitution group is $1

替换组是1美元

https://regex101.com/r/gfVkaa/1

#2


0  

with sed

$ sed -r 's/"([0-9]+\.?[0-9]*)"/\1/' file

you can fine tune to work on values only as well, but not sure needed in json format.

您可以微调以仅处理值,但不确定需要json格式。

#1


0  

To remove double quotes around "simple" numbers use the following regex pattern:

要删除“简单”数字周围的双引号,请使用以下正则表达式模式:

"(-?\d+(\.\d+)?)"

substitution group is $1

替换组是1美元

https://regex101.com/r/gfVkaa/1

#2


0  

with sed

$ sed -r 's/"([0-9]+\.?[0-9]*)"/\1/' file

you can fine tune to work on values only as well, but not sure needed in json format.

您可以微调以仅处理值,但不确定需要json格式。