2016-11-07 42 views
0

如何分离运算符上的布尔表达式? 我表达式作为以下字符串:如何分离运算符(JAVA)上的布尔表达式?

String expression = “((468551X68X304.NAOK == \"2\") and (468551X68X305.NAOK > \"2\") and (468551X68X308.NAOK != \"2000\" or 468551X68X308.NAOK > \"2000\")) “; 

我想在阵列中的所有变量,例如:

a[0] = “468551X68X304.NAOK”; 
a[1] = “468551X68X305.NAOK”; 
a[2] = “468551X68X308.NAOK”; 
a[3] = “468551X68X308.NAOK”; 

五月有人给我出个主意来解决这个问题?

谢谢,

+2

你是不是打算在这里得到帮助的丰富的,如果你不至少证明你有尝试了一些。 – rmlan

+0

我需要的是了解一些想法来解决它。也许有一些库可以获得表达式的操作符。我尝试了JEXL,但它只是运行表达式,但没有得到它的操作符。 – user3758346

+0

是的。你已经在问题中说过了。但是,这不是这个网站的工作原理。在互联网上做一些研究,编写一些代码,当你遇到上述代码的特定问题时来找我们。 – rmlan

回答

0

这可以使用正则表达式来完成。

下面的正则表达式匹配,在您的布尔表达式格式应变量的变量:

\d+X\d+X\d+\.NAOK 

,每一个\d+匹配一个或多个数字。

要使用此正则表达式中提取的变量,你可以使用java.util.regex.Patternjava.util.regex.Matcher这样的:

String booleanExpression = "((468551X68X304.NAOK == \"2\") and (468551X68X305.NAOK > \"2\") and (468551X68X308.NAOK != \"2000\" or 468551X68X308.NAOK > \"2000\")) "; 
String regex = "\\d+X\\d+X\\d+\\.NAOK"; // Note that backslash pairs don't denote two backslashes here. Because we're representing the regex as a string literal, we have to use escape sequences to represent the backslashes in the regex 

Matcher m = Pattern.compile(regex).matcher(booleanExpression); 
ArrayList<String> variables = new ArrayList<>(); 
while(m.find()) // Match a NEW variable (one that wasn't matched in previous iterations 
    variables.add(m.group()); // Add the matched variable to the ArrayList