【问题标题】:get string between parentheses [duplicate]获取括号之间的字符串[重复]
【发布时间】:2012-07-15 03:13:47
【问题描述】:

可能重复:
Regular Expression to find a string included between two characters, while EXCLUDING the delimiters

我正在使用一个名为 Interpreter.eval() 的外部库“beanshell”的方法,它允许使用字符串进行数学运算并返回解决方案。

它工作正常,但如果我想提高到二次方或从中得到一个平方根,我必须修改原来的字符串,就像

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

我需要得到括号之间的字符来替换 "(b+c)^2""(b+c)*(b+c)""Math.pow((b+c),2)"

我该怎么做? 提前致谢!

----编辑----

我终于找到了解决办法。

    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("^");

        }

    }

有了这个我修改了原来的字符串 x = 2+4*1*(2*(1+1)^2)^2+(4+5)^2 (例如)

x=2+4*1*64+81

  1. 首先搜索第一个“^”
  2. 获取前一个字符
  3. 如果有“)”
  4. 搜索前一个字符同时找到 "(" 但是如果在 "(" 之前找到 ")" 必须找到 2 个 "(";一个打开内部括号,第二个打开我要 pow 的括号。

这是在“(2+(3+4)+5)^2”的情况下--->返回“2+(3+4)+5”而不是“3+4)+5”。

现在替换为正确的表达式 Math.pow("result","2")" 并逐步计算 (1+1)^2 = 4 (2*4)^2 = 64

(4+5)^2 = 81

现在我终于可以用 Interpreter.eval() 计算返回值了

非常感谢您的回答!

【问题讨论】:

  • 你解决了吗??如果没有,请在这里评论。
  • 感谢您的评论。它可以工作,但我需要解方程,例如: x= 2/(a+b*(3*c+a)^2) ... 并且它不起作用

标签: java android regex parentheses getstring


【解决方案1】:

您需要解析字符串。您可以编写一个完整的解析器并逐个字符地检查它。

否则,如果您有特定的流程图(使用运算符),请使用 String.split(\"*(...) 功能,您基本上可以根据您的分隔符创建标记。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    当然你需要解析字符串。您可以将其转换为中间状态(之后您可以非常快速地执行)。您可以查看以下链接,也许它可以帮助您http://en.wikipedia.org/wiki/Reverse_Polish_notation

    【讨论】:

      【解决方案3】:

      试试下面的代码来完成你的任务

      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);
      

      【讨论】:

        猜你喜欢
        • 2015-10-16
        • 1970-01-01
        • 2016-03-24
        • 1970-01-01
        • 2016-08-30
        • 1970-01-01
        • 2023-03-16
        • 2018-07-30
        • 1970-01-01
        相关资源
        最近更新 更多