Reputation: 43
I was using Eclipse for Java and I want to split a number without losing other same characters.
For example, the input line is:
[1142,143,2142,142]
the output should be like that:
1142
143
2142
i was using split("142|\\D+")
but the output was showing like this:
1
143
2
What should I do ?
Upvotes: 0
Views: 41
Reputation: 2155
Replace brackets and split:
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);
Upvotes: 1
Reputation: 174696
You need to use word boundaries.
string.split("\\b142\\b|\\D+");
OR
Do replace and then split.
string.replaceAll("\\b142\\b|[\\[\\]]", "").split(",");
Upvotes: 1
Reputation: 67968
,142|\\D+
You can split by this.See demo.
https://regex101.com/r/pG1kU1/30
Upvotes: 0