Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can tokenize this string in java?

Tags:

java

regex

How can I split these simple mathematical expressions into seperate strings?

I know that I basically want to use the regular expression: "[0-9]+|[*+-^()]" but it appears String.split() won't work because it consumes the delimiter tokens as well.

I want it to split all integers: 0-9, and all operators *+-^().

So, 578+223-5^2

Will be split into:

578  
+  
223  
-  
5  
^  
2  

What is the best approach to do that?

like image 392
Mithrax Avatar asked Sep 09 '26 14:09

Mithrax


2 Answers

You could use StringTokenizer(String str, String delim, boolean returnDelims), with the operators as delimiters. This way, at least get each token individually (including the delimiters). You could then determine what kind of token you're looking at.

like image 175
Matt Avatar answered Sep 11 '26 03:09

Matt


Going at this laterally, and assuming your intention is ultimately to evaluate the String mathematically, you might be better off using the ScriptEngine

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Evaluator {
private ScriptEngineManager sm = new ScriptEngineManager();
private ScriptEngine sEngine = sm.getEngineByName("js");

public double stringEval(String expr)
{
Object res = "";
        try {
           res = sEngine.eval(expr);
          }
         catch(ScriptException se) {
            se.printStackTrace();
        }
        return Double.parseDouble( res.toString());
}

}

Which you can then call as follows:

Evaluator evr = new Evaluator();  
String sTest = "+1+9*(2 * 5)";  
double dd = evr.stringEval(sTest);  
System.out.println(dd); 

I went down this road when working on evaluating Strings mathematically and it's not so much the operators that will kill you in regexps but complex nested bracketed expressions. Not reinventing the wheel is a) safer b) faster and c) means less complex and nested code to maintain.

like image 29
Szyzygy Avatar answered Sep 11 '26 05:09

Szyzygy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!