开发者

Validate Boolean Query - java

I need the java code snippet for the below logic:

Following is a string and I need to validate the string based on the below condition :

"100 or 200 and 345 not 550 " - Valid string

"abc or 200 and 345 SAME **** 550 " - Not a Valid String

1 . the operands(eg 100,200 ..开发者_开发问答) should be positive numbers 2 . the operator should be and/or/not

Thx


You can use regular expressions in Java to do that.

Yo can try this:

public static void main(String[] args) throws Exception {
    String test = "1212 and 120 or 390";
    Pattern p = Pattern.compile("^\\d+(\\s(and|or|not)\\s\\d+)*$");
    Matcher m = p.matcher(test);
    if (m.matches()) {
        System.out.println("Valid!");
    } else {
        System.out.println("Invalid.");
    }
}


A regex is probably easiest method:

"100 or 200 and 345 not 550".matches("^[1-9][0-9]*( (or|and|not) [1-9][0-9]*)*$")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜