String replaceAll() 方法替换字符串除外
String replaceAll() method replace except string
我正在尝试使用 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,由发件人 (player/console)
输入的字符串消息创建而成
“§[0-9A-Fa-fK-Rk-r]”表示消息的颜色,在此方法中,我创建了 JSONArray,其中 link 的位置具有 click_event(每次创建一个新的 "text" 我需要发送当前颜色)
由于您要在第 5 行的正则表达式中删除 k-r
,字母 p
也会被删除,因为它在字母表中的位置在 k 和 r 之间。因此,您将永远不会在 "http" (teste.matches("http" ...
) 再往下找到匹配项。
我找到了解决方案...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();
}
我正在尝试使用 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,由发件人 (player/console)
输入的字符串消息创建而成“§[0-9A-Fa-fK-Rk-r]”表示消息的颜色,在此方法中,我创建了 JSONArray,其中 link 的位置具有 click_event(每次创建一个新的 "text" 我需要发送当前颜色)
由于您要在第 5 行的正则表达式中删除 k-r
,字母 p
也会被删除,因为它在字母表中的位置在 k 和 r 之间。因此,您将永远不会在 "http" (teste.matches("http" ...
) 再往下找到匹配项。
我找到了解决方案...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();
}