如何拆分数字而不丢失 Java 中的其他相同字符

how to split a number without losing other same characters in Java


我正在为 Java 使用 Eclipse,我想拆分一个数字而不丢失其他相同的字符。

例如输入行是:
[1142,143,2142,142]

输出应该是这样的:
1142 143 2142


我正在使用 split("142|\D+") 但输出显示如下:
1 143 2

我该怎么办?

您需要使用单词边界。

string.split("\b142\b|\D+");

先替换再拆分。

string.replaceAll("\b142\b|[\[\]]", "").split(",");
,142|\D+

您可以通过 this.See 演示拆分。

https://regex101.com/r/pG1kU1/30

替换括号并拆分:

String value = "[1142,143,2142,142]";

String xl = value.replaceAll("[\[\]]", "");

String splitted[] = xl.split(",");

for (String string : splitted)          
    if (!string.matches("142"))
        System.out.println(string);