0

我很好奇如何使用 java 的字符串正则表达式有意义地拆分带括号的数学方程。没有例子很难解释,下面是一个。

一种通用的解决方案模式将受到赞赏,而不是仅适用于下面提供的示例的模式。

String s = "(5 + 6) + (2 - 18)";
// I want to split this string via the regex pattern of "+",
// (but only the non-nested ones) 
// with the result being [(5 + 6), (2 - 18)]

s.split("\\+"); // Won't work, this will split via every plus.

我主要寻找的是第一级拆分,我想要一个正则表达式检查是否像“+”或“-”这样的符号以任何形式嵌套,如果是,不要拆分它,如果不是'不要拆分它。嵌套可以是 () 或 [] 的形式。

谢谢你。

4

3 回答 3

5

不幸的是,没有 RegEx,你需要一个像JEP这样的库

于 2013-01-13T04:12:44.830 回答
1

如果您不希望像 ((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], ]

建议改进代码。:)

于 2013-01-13T05:09:16.590 回答
0

你不能知道你永远不会得到超过一级的括号,你不能用正则表达式分析递归语法,根据定义。您需要使用或编写解析器。为 Dijkstra Shunting Yard 算法,或递归下降表达式解析器,或可以执行任一操作的库提供 aloo,

于 2013-01-13T09:19:26.620 回答