This is in regards to this thread: [ Splitting up a mathematical expression |http://forums.sun.com/thread.jspa?threadID=5358447]
and this expression:
String expression = "(5.33+13)*Sin(6)/pow(2.0,3)";
is there a way to whip up a regex statement that would split all the tokens nice and neatly via String#split(regex)? I tried creating one of my own, but I'm woefully inept at regex and it split things but also swallowed the paren, math symbols, etc...
My poor attempt:
public class MathExpression
{
public static void main(String[] args)
{
String expression = "(5.33+13)*Sin(6)/pow(2.0,3)";
String regex = "[\\+,\\-,\\*,/\\(,\\)\\,]"; // not good!
String[] tokens = expression.split(regex);
for (String string : tokens)
{
System.out.println(string);
}
}
}
thanks
Edit: or if attempting to solve this in this way is misuse of a tool and shouldn't be done -- that's a valid answer too. Again thanks.
Edited by: Encephalopathic on Jan 4, 2009 7:20 AM