如果您不希望像 ((6 + 5)-4) 这样拆分嵌套表达式,我有一个非常简单的函数可以在不使用正则表达式的情况下拆分表达式:
public static String[] subExprs(String expr) {
/* Actual logic to split the expression */
int fromIndex = 0;
int subExprStart = 0;
ArrayList<String> subExprs = new ArrayList<String>();
again:
while ((subExprStart = expr.indexOf("(", fromIndex)) != -1) {
fromIndex = subExprStart;
int substringEnd=0;
while((substringEnd = expr.indexOf(")", fromIndex)) != -1){
subExprs.add(expr.substring(subExprStart, substringEnd+1));
fromIndex = substringEnd + 1;
continue again;
}
}
/* Logic only for printing */
System.out.println("Original expression : " + expr);
System.out.println();
System.out.print("Sub expressions : [ ");
for (String string : subExprs) {
System.out.print(string + ", ");
}
System.out.print("]");
String[] subExprsArray = {};
return subExprs.toArray(subExprsArray);
}
Sample output :
原表达式:(a+b)+(5+6)+(57-6)
子表达式:[ (a+b), (5+6), (57-6), ]
编辑
对于还要获取包含在 中的表达式的额外条件[]
,此代码将处理()
和中的表达式[]
。
public static String[] subExprs(String expr) {
/* Actual logic to split the expression */
int fromIndex = 0;
int subExprStartParanthesis = 0;
int subExprStartSquareBrackets = 0;
ArrayList<String> subExprs = new ArrayList<String>();
again: while ((subExprStartParanthesis = expr.indexOf("(", fromIndex)) > -2
&& (subExprStartSquareBrackets = expr.indexOf("[", fromIndex)) > -2) {
/* Check the type of current bracket */
boolean isParanthesis = false;
if (subExprStartParanthesis == -1
&& subExprStartSquareBrackets == -1)
break;
else if (subExprStartParanthesis == -1)
isParanthesis = false;
else if (subExprStartSquareBrackets == -1)
isParanthesis = true;
else if (subExprStartParanthesis < subExprStartSquareBrackets)
isParanthesis = true;
/* Extract the sub expression */
fromIndex = isParanthesis ? subExprStartParanthesis
: subExprStartSquareBrackets;
int subExprEndParanthesis = 0;
int subExprEndSquareBrackets = 0;
if (isParanthesis) {
while ((subExprEndParanthesis = expr.indexOf(")", fromIndex)) != -1) {
subExprs.add(expr.substring(subExprStartParanthesis,
subExprEndParanthesis + 1));
fromIndex = subExprEndParanthesis + 1;
continue again;
}
} else {
while ((subExprEndSquareBrackets = expr.indexOf("]", fromIndex)) != -1) {
subExprs.add(expr.substring(subExprStartSquareBrackets,
subExprEndSquareBrackets + 1));
fromIndex = subExprEndSquareBrackets + 1;
continue again;
}
}
}
/* Logic only for printing */
System.out.println("Original expression : " + expr);
System.out.println();
System.out.print("Sub expressions : [ ");
for (String string : subExprs) {
System.out.print(string + ", ");
}
System.out.print("]");
String[] subExprsArray = {};
return subExprs.toArray(subExprsArray);
}
Sample Output :
原表达式:(a+b)+[5+6]+(57-6)-[ab]+[cd]
子表达式:[ (a+b), [5+6], (57-6), [ab], [cd], ]
建议改进代码。:)