2 回答
TA贡献1833条经验 获得超4个赞
您可以使用此包含正则表达式的代码根据您的需要解析值。
public static void main(String[] args) {
String str = "FEES_1 > 100 AND FEES_2 <= 200 OR FEES_3 <= 500";
Pattern p = Pattern.compile("(?:^|(AND|OR))\\s*(\\w+)\\s+([<>]=?)\\s+(\\d+)\\s*(?=(AND|OR|$))");
Matcher m = p.matcher(str);
for (int i = 0; m.find(); i++) {
System.out.println("Iteration " + (i + 1) + ":");
System.out.println(String.format("Operands: [%s , %s ]", m.group(2), m.group(4)));
System.out.println("Relational Operator: " + m.group(3));
System.out.println("Conditional Operator: " + m.group(1));
System.out.println();
}
}
此代码提供了与您想要的完全匹配的以下输出。
Iteration 1:
Operands: [FEES_1 , 100 ]
Relational Operator: >
Conditional Operator: null
Iteration 2:
Operands: [FEES_2 , 200 ]
Relational Operator: <=
Conditional Operator: AND
Iteration 3:
Operands: [FEES_3 , 500 ]
Relational Operator: <=
Conditional Operator: OR
TA贡献1784条经验 获得超7个赞
如果你 100% 确定输入字符串是这种格式,那么你可以简单地做这样的事情。它更简单和直接,不需要任何正则表达式。
String[] parts = string.split(" ");
按空格分割输入字符串,然后对于每个连续的迭代,您可以像这样打印
int end = (parts.length + 1)/4;
for(int i =0;i<end;i++){
System.out.println("Iteration : " + (i+1));
System.out.println("Operands: [ " + parts[4*i] + " , " + parts[4*i + 2] + "]");
System.out.println("Relational Operator: " + parts[4*i + 1]);
System.out.println("Conditional Operator: " + ((i>0)?parts[4*i - 1]:null));
}
添加回答
举报