我有这种格式的JSON文件;
[{
"item-id" : "0",
"item-name" : "Dwarf remains",
"item-examine" : "The body of a Dwarf savaged by Goblins.",
"shop-value" : "0.0",
"low-alch" : "0.0",
"high-alch" : "0.0",
"stab-bonus" : "0",
"slash-bonus" : "0",
"crush-bonus" : "0",
"magic-bonus" : "0",
"ranged-bonus" : "0",
"stab-defence" : "0",
"slash-defence" : "0",
"crush-defence" : "0",
"magic-defence" : "0",
"ranged-defence" : "0",
"strength-bonus" : "0",
"prayer-bonus" : "0",
},
{
"item-id" : "1",
"item-name" : "Toolkit",
"item-examine" : "Good for repairing a broken cannon.",
"shop-value" : "0.0",
"low-alch" : "0.0",
"high-alch" : "0.0",
"stab-bonus" : "0",
"slash-bonus" : "0",
"crush-bonus" : "0",
"magic-bonus" : "0",
"ranged-bonus" : "0",
"stab-defence" : "0",
"slash-defence" : "0",
"crush-defence" : "0",
"magic-defence" : "0",
"ranged-defence" : "0",
"strength-bonus" : "0",
"prayer-bonus" : "0",
}]
但有25 000个条目
但是在文件中有一些错误;
"prayer-bonus" : "0",
应该是
"prayer-bonus" : "0"
我如何使用正则表达式或其他只替换最后一位?
我正在使用java。
顺便说一下:值并不总是“0”
那我怎么能把它变成一个通配符所以它取代所有呢?
答案 0 :(得分:0)
使用sed:
sed -i.bak 's/\("prayer-bonus" : "0"\),/\1/' file
匹配任何数字:
sed -i.bak 's/\("prayer-bonus" : "[0-9]*"\),/\1/' file
答案 1 :(得分:0)
看起来您可能正在尝试修补由不正确形成的JSON blob引起的问题。您可以采用其他响应者使用sed来解决这些症状的答案,但问题在于如何创建JSON。
答案 2 :(得分:0)
任何文本编辑器都可以解决问题。
或者你可以从java调用字符串替换。不需要正则表达式。
答案 3 :(得分:0)
你可以使用它(用java风格编写,与replaceAll()
方法一起使用):
search: (?<!\\\\)(\"(?>[^\\\"]++|\\\\{2}|\\\\.)*+\"),(\\s*+})
replace: $1$2
这种模式的主要兴趣在于避免使用可能在引号内的双引号转义。
示例:
String pattern = "(?<!\\\\)(\"(?>[^\\\"]++|\\\\{2}|\\\\.)*+\"),(\\s*+})";
String result = yourJson.replaceAll(pattern, "$1$2");