【问题标题】:Evaluating math expressions in a string using regex to split the string使用正则表达式评估字符串中的数学表达式以拆分字符串
【发布时间】:2017-02-13 18:13:28
【问题描述】:

我需要一些用于评估字符串中的数学表达式。

到目前为止,我的代码仅适用于正数。

我使用正则表达式将字符串拆分为两个单独的数组。我能够将所有数学符号拆分为一个数组,将所有数字拆分为另一个数组。但我不确定如何处理负数。 (我不明白正则表达式,我只是放了东西,它有效,但不适用于负数)

这里是我的代码,提前致谢!`

    boolean mybool = true;
    String str = "1.0+2.0+3.0+4.0";
    String[] numarray = str.split("[-+/%*]");
    String[] signarray = str.split("[0123456789.]");
    double result = 0.0;
    double num1 = 0.0;
    double num2 = 0.0;

    String mystr = "";

    //Adds each element in sign array to mystr
    for(String e : signarray){
        if(e != ""){
            mystr+=e;
        }
    }

    //Assign signarray new size of length mystr
    signarray = new String[mystr.length()];


    //Cycle through each element in str and add it to signarray
    for(int i = 0; i < mystr.length(); i++){
        signarray[i] = mystr.charAt(i)+"";
    }

    //Print each element in num array
    System.out.print("Print each element in number array: ");
    for(String e : numarray){
        System.out.print(e+ " ");
    }
    System.out.println();

    System.out.print("Print each element in sign array: ");
    //print each element in sign array
    for(String e : signarray){
        System.out.print(e+ " ");
    }
    System.out.println();

    //Prints each element in sign array and element value
    for(int i = 0; i < signarray.length; i++){
        System.out.println("SignArray[" + i + "] = " + signarray[i]);
    }


    for(int i = 2; i <= numarray.length; i++){

        //this will get the first two indexes of number array
        //and store them in num1 and num1 then i use another if
        //statement to go sign array to get a sign to evaluate the
        //two nums and store the value in result.
        //hopefully you understand my logic
        if(mybool == true){


            num1 = Double.parseDouble(numarray[0]);
            num2 = Double.parseDouble(numarray[1]);
            System.out.println("num1 = " + num1);
            System.out.println("num2 = " + num2);


            if(signarray[0].equals("+")){
                result = num1 + num2;
                System.out.println("Result = num1 + num2 = " + num1 + "+" + num2 + "= " + result );
            } else if(signarray[0].equals("-")){
                result = num1 - num2;
                System.out.println("Result = num1 - num2 = " + num1 + "-" + num2 + "= " + result );
            } else if(signarray[0].equals("/")){
                result = num1 / num2;
                System.out.println("Result = num1 / num2 = " + num1 + "/" + num2 + "= " + result );
            } else if(signarray[0].equals("*")){
                result = num1 * num2;
                System.out.println("Result = num1 * num2 = " + num1 + "*" + num2 + "= " + result );
            } else if(signarray[0].equals("%")){
                result = num1 % num2;
                System.out.println("Result = num1 % num2 = " + num1 + "%" + num2 + "= " + result );
            }

            mybool = false;

        } else {

            num2 = Double.parseDouble(numarray[i-1]);
            System.out.println("Num2 = " + num2);


            if(signarray[i-2].equals("+")){
                result = result + num2;
                System.out.println("Result after math is : " + result);

            } else if(signarray[i-2].equals("-")){
                result = result - num2;
                System.out.println("Result after math is : " + result);
            } else if(signarray[i-2].equals("/")){
                result = result / num2;
                System.out.println("Result after math is : " + result);
            } else if(signarray[i-2].equals("*")){
                result = result * num2;
                System.out.println("Result after math is : " + result);
            } else if(signarray[i-2].equals("%")){
                result = result % num2;
                System.out.println("Result after math is : " + result);
            }

        }



    }`

输出:

Print each element in number array: 1.0 2.0 3.0 4.0 
Print each element in sign array: + + + 
SignArray[0] = +
SignArray[1] = +
SignArray[2] = +
num1 = 1.0
num2 = 2.0
Result = num1 + num2 = 1.0+2.0= 3.0
Num2 = 3.0
Result after math is : 6.0
Num2 = 4.0
Result after math is : 10.0

最终我希望能够评估这样的字符串 //字符串str = "3.01+2.2/4.01*7.1%4.0--2.0";

但我不知道如何从 sting 中获取负数并存储在 num 数组中。

感谢您的帮助!

【问题讨论】:

  • --thould throw -(-2) 会很好
  • 我会为您的正则表达式调查正面和负面的后视。或者更好的是,不要使用正则表达式,而是使用解析器。
  • 如果你在标志上分裂,你永远不会得到标志。此外,--2.0-1 * -2.0
  • 您不应该为此使用正则表达式。您需要一个合适的扫描仪,然后是一个表达式解析器。抛开一切,查找 Disjkstra Shunting-yard 算法或递归下降表达式解析。

标签: java regex string


【解决方案1】:

理想情况下,您应该使用解析器而不是正则表达式。但是,鉴于您当前的要求,使用正面和负面的lookbehinds 会很简单。

(1)。运算符前面总是有一个十进制数。因此,我们通过匹配出现在十进制数之后的任何运算符来拆分参数(正向后视)。

String[] arguments = str.split("(?<=\\d)[-+/%*]");

(2)。参数可以以可选的减号开头,但不能以另一个十进制数字开头。因此,我们通过匹配不在十进制数之后的参数来拆分运算符(负向后查找)。

String[] operators = str.split("(?<!\\d)-?[0-9.]+");

但是请注意,在数组的位置 0 会有一个空运算符。如果您想避免这种情况,那么您可以使用许多不同的方法来代替 String.split。

【讨论】:

    猜你喜欢
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 2017-02-23
    相关资源
    最近更新 更多