I am trying to write a method that would split up an inputted math expression into all the individual operators and numbers. For example, 1 + 1 would produce an array of { 1, +, 1 } and 2 - -2 would produce an array of { 2, -, -2 }. I sort of got this working after doing some research on StackOverflow (using this question: Splitting a simple maths expression with regex) and I wrote the following:
String[] t = tokens.split("(?<=[-+*/()])|(?=[-+*/()])");
This works great with the 1 + 1, but doing 2 - -2 produces { 2, -, -, 2 }, which is not what I want. I am kind of new to splitting and regexes, so this may be a basic question that I am simply failing to properly research, but any help would be greately appreciated! Also, I realize it would be super easy to just separate by spaces, but I'd really like to avoid that. Thanks a lot for any help!