[英]Java convert String to logic
//having Java 7/8: String logic = "123 OR 345 OR 678 NAND 901"; //具有Java 7/8:字符串逻辑=“ 123 OR 345 OR 678 NAND 901”;
//Should evaluate something simililar to this:
String test1 = "abc345678opq"; String test2 = "abc123901";
boolean isCheckOk1 = test1.contains("123") || test1.contains("345") || test1.contains("678") && !test1.contains("901");
boolean isCheckOk2 = test2.contains("123") || test2.contains("345") || test2.contains("678") && !test2.contains("901");
//where isCheckOk1 will be true
//and isCheckOk2 will be false;
Is there a generic way in Java a library to achive this? Java中是否有通用的方法可以实现此目的的库?
Next level: 下一级:
"123 OR 345 OR 678 NAND (901 AND 902)"
Thank you! 谢谢!
No, but you could use regex: 不,但是您可以使用正则表达式:
This: 这个:
boolean isCheckOk1 = test1.contains("123") || test1.contains("345") || test1.contains("678") && !test1.contains("901");
can be coded as: 可以编码为:
boolean isCheckOk1 = test1.matches("(?!.*901).*(123|345|678).*");
The "extra points" could be coded as: “额外点”可以编码为:
boolean isCheckOk1 = test1.matches("(?!.*901.*902)(?!.*902.*901).*(123|345|678).*");
Complicated logic would become unwieldy to do in regex. 在正则表达式中,复杂的逻辑将变得难以处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.