Reputation: 131
I am trying to split a string with delimiters into an array while keeping the delimiters as well.
The string that I have is: "2+37/4+26".
I want the array to be: [2,+,37,/,4,+,26]
Upvotes: 3
Views: 870
Reputation: 784958
You can split using lookarounds:
String[] tok = input.split("(?<=[+*/-])|(?=[+*/-])");
Explanation:
(?<=[+*/-]) # when preceding character is one of 4 arithmetic operators
| # regex alternation
(?=[+*/-]) # when following character is one of 4 arithmetic operators
Upvotes: 3