字符串replaceAll()方法替换除字符串

时间:2018-05-21 05:31:00

标签: java string replaceall

我正在尝试使用replaceAll()方法替换除字符串序列之外的字符串上的所有内容,但是我没有使它工作,我有以下代码:

基本上:

String message = "§eTeste"

String color = message.replaceAll(?, ""); // The problem

System.out.println(color);

输出应为“§e”

代码:

        String[] args = message.split(" ");
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < args.length; i++) {
            String teste = args[i].replaceAll("§[0-9A-Fa-fK-Rk-r]", "");

            if (i == (args.length-1)) {
                if (teste.matches("http://" + "(.*)") || teste.matches("https://" + "(.*)")) {

                    String cor = "";
                    if (!args[i].matches("(.*)" + "§[0-9A-Fa-fK-Rk-r]" + "(.*)") && (sb.toString() !=null) && !sb.toString().isEmpty()) {
                        cor = sb.toString().replaceAll("[^(§[0-9A-Fa-fK-Rk-r])]", "");
                    }

                    if ((sb.toString() !=null) && !sb.toString().isEmpty()) {
                        JSONObject objeto = new JSONObject();

                        objeto.put("text", sb.toString());
                        jmessage.add(objeto);
                    }

                    JSONObject objeto = new JSONObject();
                    JSONObject extra = new JSONObject();

                    extra.put("action", "open_url");
                    extra.put("value", teste);

                    objeto.put("text", cor + args[i]);
                    objeto.put("clickEvent", extra);

                    jmessage.add(objeto);
                }
                else {
                    JSONObject objeto = new JSONObject();

                    sb.append(args[i]);

                    objeto.put("text", sb.toString());
                    jmessage.add(objeto);
                }
            }
            else {
                if (teste.matches("http://" + "(.*)") || teste.matches("https://" + "(.*)")) {

                    String cor = "";
                    if (args[i].matches("(.*)" + "§[0-9A-Fa-fK-Rk-r]" + "(.*)")) {
                        cor = args[i].replaceAll("[^(§[0-9A-Fa-fK-Rk-r])]", "");
                    }
                    else if (sb.toString() !=null) {
                        cor = sb.toString().replaceAll("[^(§[0-9A-Fa-fK-Rk-r])]", "");
                    }

                    if ((sb.toString() !=null) && !sb.toString().isEmpty()) {
                        JSONObject objeto = new JSONObject();

                        objeto.put("text", sb.toString());
                        jmessage.add(objeto);

                        sb = new StringBuilder();
                        sb.append(" " + cor);
                    }

                    JSONObject objeto = new JSONObject();
                    JSONObject extra = new JSONObject();

                    objeto.put("text", cor + args[i]);

                    extra.put("action", "open_url");
                    extra.put("value", teste);

                    objeto.put("clickEvent", extra);

                    jmessage.add(objeto);
                }
                else {
                    sb.append(args[i] + " ");
                }
            }
        }

jmessage是一个JSONArray创建的消息,是由发送者(播放器/控制台)发出的字符串输入

“§[0-9A-Fa-fK-Rk-r]”代表消息的颜色,在这个methot我正在创建JSONArray,其中link的位置有click_event(每次创建一个新的“text”我需要发送它的当前颜色)

2 个答案:

答案 0 :(得分:0)

由于您要在第5行的正则表达式中删除k-r,因此字母p也会被删除,因为它在k和r之间的字母表中的位置。因此,您将永远不会在“http”(teste.matches("http" ...)上获得匹配。

答案 1 :(得分:0)

我找到了一个解决方案...... replaceAll()方法不能超过1个字符..因为我已经创建了这个方法来解决问题:

private String findCor(String string) {
    char[] b = string.toCharArray();

    StringBuilder sb = new StringBuilder();
    sb.append("");

    for (int i = 0; i < (b.length-1); i++) {
        if ((b[i] == ChatColor.COLOR_CHAR) && ("0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[(i + 1)]) > -1)) {
            sb.append(b[i]);
            sb.append(b[(i + 1)]);
        }
    }
    return sb.toString();
}
相关问题