Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Splitting math expression, part 2: regex

807589Jan 4 2009 — edited Jan 5 2009
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 2 2009
Added on Jan 4 2009
5 comments
1,247 views