I want to recognize integers or decimals and the four simple operations, broken into tokens I can\'t get the decimal to work, can you please help?
My reg is
It may works for you:
expression = "2.7 + 3 * (1 + 2)";
String expRegString = "\\d+(\\.\\d+)*|\\(\\d+(\\.\\d+)*[\\+\\-\\*\\/]\\d+(\\.\\d+)*\\)";
You can try removing all spaces and then split your data on every place that is before or after characters -
+
*
/
(
)
.
This should do the trick
String expression = "2.7 + 3 * (1 + 2)";
String[] tokens = expression.replaceAll("\\s+", "").split("(?<=[-+*/()])|(?=[-+*/()])");
for (String token : tokens)
System.out.println(token);
Output
2.7
+
3
*
(
1
+
2
)