How to split a string using regex, taking numbers with more of 1 digits #Java

One interpretation of the question is that you want to keep digits together, but split everything else into separate tokens, so you want to split:

  • After a digit
  • Before a digit
  • Not between digits

As independent rules, that would be:

  • After a digit that is not followed by a digit: (?<=\d)(?!\d)
  • Before a digit is not preceded by a digit: (?<!\d)(?=\d)

So what you want is:

String[] somma = text1.split("(?<=\\d)(?!\\d)|(?<!\\d)(?=\\d)");

To not get an empty token at the beginning, we don’t want to split “before a digit that is at the beginning of input”, so we add that exact condition:

String[] somma = text1.split("(?<=\\d)(?!\\d)|(?<!\\d|^)(?=\\d)");
// result: ["6", "+", "9", "-", "2", "+", "4", "-", "12"]

This will allow evaluating an expression consisting of all operators, incl. parentheses, depending on the code that uses the result of this tokenization of the input expression.


Another interpretation of the question, based on the attempted split in the question, is that you only want to split after digits, so you want to split:

  • After a digit
  • Not between digits

That is of course even simpler:

String[] somma = text1.split("(?<=\\d)(?!\\d)");
// result: ["6", "+9", "-2", "+4", "-12"]

This will allow evaluating an expression consisting of only + and - operators, by simply parsing and adding the values.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top