gpt4 book ai didi

java - 字符串替换屏蔽 json 文件的更有效方法 (java)

转载 作者:行者123 更新时间:2023-12-02 12:12:18 24 4
gpt4 key购买 nike

我有一些代码来屏蔽 json 文件中的值。但我觉得应该有一种更有效的方法来做到这一点。

这是我当前的代码:

private static final String STRING_REPLACEMENT = "$1 \"String\" $4";
private static final String FLOAT_REPLACEMENT = "$1 \"float\" $4";
private static final String INT_REPLACEMENT = "$1 \"Integer\" $4";
private static final String BOOL_REPLACEMENT = "$1 \"Boolean\" $4";

以及实际的屏蔽代码:

out = out.replaceAll("(\"\\w+\"\\s?:)\\s?\"((\\\\\"|[^\"])*\")", "$1 \"String\"");
out = out.replaceAll("(\"\\w+\"\\s?:)\\s?(\\d+\\.\\d+)", "$1 \"float\"");
out = out.replaceAll("(\"\\w+\"\\s?:)\\s?(\\d+)", "$1 \"Integer\"");
out = out.replaceAll("(\"\\w+\"\\s?:)\\s?(true|false)", "$1 \"Boolean\"");

out = out.replaceAll("(\\[\\s?)\"((\\\\\"|[^\"])*\")(\\s?,)", STRING_REPLACEMENT);
out = out.replaceAll("(,\\s?)\"((\\\\\"|[^\"])*\")(\\s?,)", STRING_REPLACEMENT);
out = out.replaceAll("(,\\s?)\"((\\\\\"|[^\"])*\")(\\s?\\])", STRING_REPLACEMENT);

out = out.replaceAll("(\\[\\s?)((\\d)+\\.\\d+)(\\s?,)", FLOAT_REPLACEMENT);
out = out.replaceAll("(,\\s?)((\\d)+\\.\\d+)(\\s?,)", FLOAT_REPLACEMENT);
out = out.replaceAll("(,\\s?)((\\d)+\\.\\d+)(\\s?\\])", FLOAT_REPLACEMENT);

out = out.replaceAll("(\\[\\s?)((\\d)+)(\\s?,)", INT_REPLACEMENT);
out = out.replaceAll("(,\\s?)((\\d)+)(\\s?,)", INT_REPLACEMENT);
out = out.replaceAll("(,\\s?)((\\d)+)(\\s?\\])", INT_REPLACEMENT);

out = out.replaceAll("(\\[\\s?)((true|false))(\\s?,)", BOOL_REPLACEMENT);
out = out.replaceAll("(,\\s?)((true|false))(\\s?,)", BOOL_REPLACEMENT);
out = out.replaceAll("(,\\s?)((true|false))(\\s?\\])", BOOL_REPLACEMENT);

有人对如何改进这个有任何建议吗?我知道我可以对正则表达式进行一些分组,但我担心这会影响 much 的可读性。我注意到,如果我将 3 条正则表达式行合并为一行,并添加更多行?因为它并不总是以正确的方式找到它,因此屏蔽会失败。

最佳答案

您可以使用正则表达式/替换创建 HashMap 并迭代以应用您的replaceAll

例如

import java.util.HashMap;
import java.util.Map;

public class Test {

private static final String STRING_REPLACEMENT = "$1 \"String\" $4";
private static final String FLOAT_REPLACEMENT = "$1 \"float\" $4";
private static final String INT_REPLACEMENT = "$1 \"Integer\" $4";
private static final String BOOL_REPLACEMENT = "$1 \"Boolean\" $4";
private static final Map<String,String> patterns;
static {
patterns = new HashMap<>();
patterns.put("(\"\\w+\"\\s?:)\\s?\"((\\\\\"|[^\"])*\")", "$1 \"String\"");
patterns.put("(\"\\w+\"\\s?:)\\s?(\\d+\\.\\d+)", "$1 \"float\"");
patterns.put("(\"\\w+\"\\s?:)\\s?(\\d+)", "$1 \"Integer\"");
patterns.put("(\"\\w+\"\\s?:)\\s?(true|false)", "$1 \"Boolean\"");
patterns.put("(\\[\\s?)\"((\\\\\"|[^\"])*\")(\\s?,)", STRING_REPLACEMENT);
patterns.put("(,\\s?)\"((\\\\\"|[^\"])*\")(\\s?,)", STRING_REPLACEMENT);
patterns.put("(,\\s?)\"((\\\\\"|[^\"])*\")(\\s?\\])", STRING_REPLACEMENT);
patterns.put("(\\[\\s?)((\\d)+\\.\\d+)(\\s?,)", FLOAT_REPLACEMENT);
patterns.put("(,\\s?)((\\d)+\\.\\d+)(\\s?,)", FLOAT_REPLACEMENT);
patterns.put("(,\\s?)((\\d)+\\.\\d+)(\\s?\\])", FLOAT_REPLACEMENT);
patterns.put("(\\[\\s?)((\\d)+)(\\s?,)", INT_REPLACEMENT);
patterns.put("(,\\s?)((\\d)+)(\\s?,)", INT_REPLACEMENT);
patterns.put("(,\\s?)((\\d)+)(\\s?\\])", INT_REPLACEMENT);
patterns.put("(\\[\\s?)((true|false))(\\s?,)", BOOL_REPLACEMENT);
patterns.put("(,\\s?)((true|false))(\\s?,)", BOOL_REPLACEMENT);
patterns.put("(,\\s?)((true|false))(\\s?\\])", BOOL_REPLACEMENT);
}
public void test() {
String out="your string";
for(Map.Entry<String, String> pattern :patterns.entrySet()){
out = out.replaceAll(pattern.getKey(), pattern.getValue());
}
}
}

但是,最好反序列化、更新值并使用 api 重新序列化您的 json

关于java - 字符串替换屏蔽 json 文件的更有效方法 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46447176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com