get string between parentheses

Dors picture Dors · Jul 16, 2012 · Viewed 8.1k times · Source

Possible Duplicate:
Regular Expression to find a string included between two characters, while EXCLUDING the delimiters

I'm using a method called Interpreter.eval() of external library "beanshell" that allows to make math operations with Strings and return the solution.

It works fine, but if I want to raise to the second power or get a square root out of this I have to modify the original String that is like

String st ="x = 2+4*a*(b+c)^2"

I need to get the charaters between parentheses to replace "(b+c)^2" to "(b+c)*(b+c)" or "Math.pow((b+c),2)"

How can I do this? Thanks in advance!

----edit----

Finally I found the solution.

    Interpreter interpreter = new Interpreter();
    String st = "x = 2+4*1*(2*(1+1)^2)^2 + (4+5)^2";

    int index = -2;
    char prev;
    char post;
    int openPar = -1;
    if (st.contains("^")) {

        index = st.indexOf("^");

        while (index != -1) {

            prev = st.charAt(index - 1);
            post = st.charAt(index + 1);

            if (prev == ')') {
                int match = 0;
                int i = index - 2;
                char check = '0';

                boolean contiunar = true;
                while (contiunar) {
                    check = st.charAt(i);

                    if (check == ')')
                        match++;

                    if (check == '(') {

                        if (match == 0) {

                            openPar = i;
                            contiunar = false;
                        } else
                            match = match - 1;
                    }

                    i = i - 1;

                }

                String rep = st.substring(openPar + 1, index - 1);
                String resultado = "";
                try {
                    interpreter.eval("r= Math.pow(" + rep + "," + post
                            + ")");
                    resultado = interpreter.get("r").toString();

                } catch (EvalError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                st = st.replace("(" + rep + ")^" + post, resultado);

            } else {
                st = st.replace(prev + "^" + post, prev + "*" + prev);
            }

            index = st.indexOf("^");

        }

    }

With this I modified the original String x = 2+4*1*(2*(1+1)^2)^2+(4+5)^2 (for example)

to x=2+4*1*64+81

  1. first it search the first "^"
  2. get previous char
  3. and if there are ")"
  4. search previous char while finds "(" But if finds ")" before of "(" has to find 2 "("; one to open internal parentheses and the second open the parentheses I want to pow.

This is in case of "(2+(3+4)+5)^2" ---> return "2+(3+4)+5" instead of "3+4)+5".

now replace to correct expresion Math.pow("result","2")" and calculate step by step (1+1)^2 = 4 (2*4)^2 = 64

(4+5)^2 = 81

finally now I can calculate the retun with Interpreter.eval()

Thanks a lot for the answers!

Answer

sunil picture sunil · Jul 16, 2012

try the following code to do your task

String originalString ="x = 2+4*a*(b+c)^2";
String stringToInsert = "Math.pow((b+c),2)";

int startIndex = originalString.indexOf("(");
int endIndex = originalString.indexOf(")");

String firstPortion = originalString.substring(0,startIndex);
String lastPortion = originalString.substring(endIndex+1);
String finalString = firstPortion + stringToInsert + lastPortion;

System.out.println("finalString : "+finalString);