Is it possible to compute a boolean expression to compare strings?

I will have a string like

('abc' != 'xyz' AND 'thy' = 'thy') OR ('ujy' = 'ujy')

A string will be able to have as many AND groups as it wants. There will be no nested groups in AND groups. All groups will ALWAYS be processed by OR.

I can just turn off AND for &&and OR for ||.

I would like to pass this String to some type of eval method and print TRUE or FALSE.

Is there anything that can do this?

+4
source share
4 answers

You can use the built-in Javascript engine that ships with JDK1.6 to evaluate a string containing mathematical expressions.

You can find here: ScriptEngine

Here is an example:

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

public class Myclass {
    public static void main(String[] args) {

        try {

            ScriptEngineManager sem = new ScriptEngineManager();
            ScriptEngine se = sem.getEngineByName("JavaScript");
            String myExpression = "('abc' == 'xyz' && 'thy' == 'thy') || ('ujy' == 'ujy')";
            System.out.println(se.eval(myExpression));

        } catch (ScriptException e) {

            System.out.println("Invalid Expression");
            e.printStackTrace();

        }
    }
}

:

'AND' '& &',
'OR' '||',
'=' '=='

javax.script.ScriptException

+13

script ,

    ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
    String[] vars = {"var1 = 'xyz'", "var2 = 'xyz'"};

    try {
        for (String var : vars) {
            engine.eval(var);
        }
        System.out.println(engine.eval("var1 == var2 "));
    } catch (ScriptException e) {
        e.printStackTrace();
    }
+2

,

public interface StringEquals{

    public boolean equals(String s1, String s2);
}

public class Equals implements StringEquals{

    private String mS1;
    private STring mS2;

    public NotEquals(String s1, String s2){
        mS1 = s1;
        mS2 = s2;
    }


    public boolean equals(){
        return mSq1.equals(mS2);
    }
}

public class NotEquals implements StringEquals{

    private String mS1;
    private STring mS2;

    public NotEquals(String s1, String s2){
        mS1 = s1;
        mS2 = s2;
    }

    public boolean equals(){
        return !mS1.equals(mS2);
    }
}

public class AndGroup{

    private List<StringEquals> mStrings;

    public AndGroup(List<StringEquals> list){
        mStrings = list;
    }

    public boolean getResult(){
        for(StringEquals e: mStrings){
            if (!e.equals()){
                return false;
            }
        }
        return true;
    }



and a class to parse it:

public boolean eval(String evalString){
    List<AndGroup> groups = new LinkedList<AndGroup>();
    String[] ands = evalString.split("OR");
    for (String andExp : ands){
        List<StringEquals> list = new LinkedList<StringEquals>();
        String compares = andExp.split("AND");
        for (String comp: compares){
            if (comp.contains("!="){
                String[] notEqual = comp.split("!=");
                list.add(new NotEquals(notEqual[0], notEqual[1]));
            } else {
                 String[] equal = comp.split("=");
                 list.add(new Equals(equal[0], equal[1]);
            }
        }
        groups.add(new AndGroup(list));


    }
    for (AndGroup g: groups){
        if (g.getResult()){
            return true;
        }
    }
    return false;

}

,

+1

, groovy ( ) groovy.util.Eval, AND\OR && ||.

0

All Articles