0


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 ?

2
  • why not split it with a comma. String [] tokens = input.split(","); Commented Jun 3, 2015 at 9:56
  • hi @UmaLakshmiKanth, because i want to split the number 142 only~ Commented Jun 3, 2015 at 10:02

3 Answers 3

1

You need to use word boundaries.

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

OR

Do replace and then split.

string.replaceAll("\\b142\\b|[\\[\\]]", "").split(","); 
Sign up to request clarification or add additional context in comments.

Comments

1

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); 

Comments

0
,142|\\D+ 

You can split by this.See demo.

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

1 Comment

This solution is not general enough.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.