使用正则表达式来评估字符串中的数学表达式以分割字符串

时间:2017-02-13 18:13:28

标签: java regex string

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

截至目前,我的代码仅适用于正数。

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

无论如何这里是我的代码,提前谢谢!`

    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

最终我想能够评估这样的字符串 // String str =“3.01 + 2.2 / 4.01 * 7.1%4.0--2.0”;

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

谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

理想情况下,您应该使用解析器而不是正则表达式。然而,考虑到您目前的要求,使用正面和负面的外观是很简单的。

(1)。运算符前面总是以十进制数表示。因此,我们通过匹配出现在十进制数字后面的任何运算符(正向后观)来拆分参数。

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

(2)。参数可以以可选的减号开头,但从不以另一个十进制数开头。因此,我们通过匹配不在十进制数后面的参数来分割运算符(负面的后观)。

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

但请注意,数组的第0位会有一个空操作符。如果你想避免这种情况,那么可以使用许多不同的方法而不是String.split。