title: get string between parentheses
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1454913/regular-expression-to-find-a-string-included-between-two-characters-while-exclu">Regular Expression to find a string included between two characters, while EXCLUDING the delimiters</a>  </p>
  </blockquote>
  
  
  
  <p>I'm using a method called Interpreter.eval() of  external library "beanshell" that allows to make math operations with Strings and return the solution.</p>
  
  <p>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</p>
  
  <p><strong>String st ="x = 2+4*a*(b+c)^2"</strong></p>
  
  <p>I need to get the charaters between parentheses to replace <strong>"(b+c)^2"</strong> to <strong>"(b+c)*(b+c)"</strong> or <strong>"Math.pow((b+c),2)"</strong></p>
  
  <p>How can I do this?
  Thanks in advance!</p>
  
  <p>----edit----</p>
  
  <p>Finally I found the solution.</p>
  
  <pre><code>    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("^");
  
          }
  
      }
  </code></pre>
  
  <p>With this I modified the original String <strong>x = 2+4*1*(2*(1+1)^2)^2+(4+5)^2</strong>   (for example)</p>
  
  <p>to <strong>x=2+4*1*64+81</strong></p>
  
  <ol>
  <li>first it search the first "^"</li>
  <li>get previous char</li>
  <li>and if there are ")"</li>
  <li>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.</li>
  </ol>
  
  <p>This is in case of "(2+(3+4)+5)^2"  ---> return "2+(3+4)+5"  instead of "3+4)+5".</p>
  
  <p>now replace to correct expresion <strong>Math.pow("result","2")"</strong> and calculate step by step
  (1+1)^2 = 4
  (2*4)^2 = 64</p>
  
  <p>(4+5)^2 = 81</p>
  
  <p>finally now I can calculate the retun with Interpreter.eval()</p>
  
  <p>Thanks a lot for the answers!</p>