如何从文件中删除特定字符? [英] How do i remove a specefic character from a file?

查看:143
本文介绍了如何从文件中删除特定字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种格式的JSON文件;

I have this JSON file in this format;

[{
"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个条目

But with 25 000 entries

但在文件中有错误;

"prayer-bonus" : "0",

应为

"prayer-bonus" : "0"

我如何使用正则表达式或任何只替换最后一位?

How can i use regex or whatever to only replace the last bit?

Im使用java。

顺便说一句:该值不总是0

By the way: the value is not always "0"

那么我该如何做一个通配符, p>

So how can i have it be a wildcard so it replaces all ?

推荐答案

您可以使用这个(用java风格编写,用于 replaceAll() method):

You can use this (written in java style, to use with replaceAll() method):

search: (?<!\\\\)(\"(?>[^\\\"]++|\\\\{2}|\\\\.)*+\"),(\\s*+})
replace: $1$2

示例:

String pattern = "(?<!\\\\)(\"(?>[^\\\"]++|\\\\{2}|\\\\.)*+\"),(\\s*+})";
String result = yourJson.replaceAll(pattern, "$1$2");

这篇关于如何从文件中删除特定字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆