Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
环绕括号标识符Java_Java_Regex_String_Char_Iteration - Fatal编程技术网

环绕括号标识符Java

环绕括号标识符Java,java,regex,string,char,iteration,Java,Regex,String,Char,Iteration,如果您能用Java帮助我,我将不胜感激 给定两个字符串,假设stringa=“(A+B)+(C)”和stringb=“((A+B)+(C))”和stringc=(A+B)和stringd=A+(B+C)和stringe=(A+(B+C)) 如何识别字符串是否完全由类似字符串B的括号包围 例如:boolean标志(String expr){//如果被包围,则返回false,否则返回true} 如果expr=A,则标志将返回true 如果expr=B,则标志将返回false 如果expr=C,则标志

如果您能用Java帮助我,我将不胜感激

给定两个字符串,假设
stringa=“(A+B)+(C)”
stringb=“((A+B)+(C))”
stringc=(A+B)
stringd=A+(B+C)
stringe=(A+(B+C))

如何识别字符串是否完全由类似字符串B的括号包围

例如:
boolean标志(String expr){//如果被包围,则返回false,否则返回true}

如果
expr=A
,则标志将返回true

如果
expr=B
,则标志将返回false

如果
expr=C
,则标志将返回false

如果
expr=D
,则标志将返回true

如果expr=E,flag将返回flase

抱歉,如果不清楚,但它应该适用于任何字符串表达式:

假设表达式只包含数字运算符括号


谢谢。非常感谢。

你不能用正则表达式来做,因为嵌套括号不是一种正则语言

相反,迭代字符串并通过计算左括号和右括号的数量来跟踪嵌套级别。对于每个左括号,向嵌套级别添加一个。每个右括号减去一个

  • 如果在到达字符串末尾之前达到零(或更少),则返回true
  • 如果最后达到零,则返回false
  • 其他任何事情都是不平衡的括号,除非输入无效,否则不应该发生
以下是一些工作示例来演示该原理:

(A+B)+(C)
11110        TRUE

((A+B)+(C))
12222112210  FALSE

(A+B)
11110        FALSE

A+(B+C)
0            TRUE

(A+(B+C))
111222210    FALSE

*明智地

我认为在你的情况下有两种选择

  • 使用子串方法
  • 例如:

    public boolean checkForParanthesis(String str) {
     Integer last = str.length() - 1; // Get number of the last character
     String firstChar = str.substring(0); // Get first character of the string
     String lastChar = str.substring(last); // Get last character of the string
     if (firstChar.equals("(") && lastChar.equals(")")) return false;
     return true
    }
    
  • 使用正则表达式。也许这是一个更好的解决办法

  • 马克·拜尔斯的算法似乎与您所寻找的大致相同。现在要把它放在一起,您必须使用
    for
    if
    Java关键字和索引。下面的代码就是一个例子。但是,它不会验证表达式,因此在测试例如
    A+B)
    无效表达式时不会抛出错误(只返回
    true
    值)。检查并亲自测试。希望这有点帮助

    
    package test;
    
    public class Main {
    
      public static void main(String[] args) {
        Main m = new Main();
        m.start();
      }
    
      private void start() {
        /* true */
        System.out.println(isNotSurrounded("A"));
        System.out.println(isNotSurrounded("A+B"));
        System.out.println(isNotSurrounded("A+(B+C)"));
        System.out.println(isNotSurrounded("(B+C)+D"));
        System.out.println(isNotSurrounded("A+(B+C)+D"));
        System.out.println(isNotSurrounded("(A+B)+(C)"));
        System.out.println(isNotSurrounded("(A)+(B)+(C)"));
        System.out.println(isNotSurrounded("(A)+((B)+(C))+(D+E+F+(G))"));
        /* false */
        System.out.println();
        System.out.println(isNotSurrounded("(A)"));
        System.out.println(isNotSurrounded("(A+B)"));
        System.out.println(isNotSurrounded("(A+(B+C))"));
        System.out.println(isNotSurrounded("((B+C)+D)"));
        System.out.println(isNotSurrounded("(A+(B+C)+D)"));
        System.out.println(isNotSurrounded("((A+B)+(C))"));
        System.out.println(isNotSurrounded("((A)+(B)+(C))"));
        System.out.println(isNotSurrounded("((A)+((B)+(C))+(D+E+F+(G)))"));
      }
    
      private boolean isNotSurrounded(String expression) {
        if (expression.startsWith("(") && expression.endsWith(")") && expression.length() > 2) {
          int p = 0;
          for (int i = 1; i < expression.length() - 1; i++) {
            if (expression.charAt(i) == '(') {
              p++;
            } else if (expression.charAt(i) == ')') {
              p--;
            }
            if (p < 0) {
              return true;
            }
          }
          if (p == 0) {
            return false;
          }
        }
        return true;
      }
    }
    

    (A+B)被包围,所以它应该返回false哦,对了,对不起,我从后面到前面得到了true和false:您想检测字符串何时没有完全被括号包围。我将更新我的答案。上面所有3个案例都将返回false对于
    a+(B+C)
    表达式的返回值如何?这是一个常规用例吗?是的,一个应该返回true,因为它不是被包围的。他可能是这个意思,但现在提出的问题是,我们只关心一个非常琐碎的语句,它是否被包围(而不是paren是否正确匹配)。与
    \(.*)
    或类似的匹配很好。@Jiri不,他不是,他在解决一个与你认为应该解决的问题略有不同的问题。这个问题的措辞很简单。。次优。Voo,我想你错了,因为
    (A)+(B)
    返回的值与
    (A+B)
    不同。在您的解决方案中,它将始终返回相同的值。
    
    true
    true
    true
    true
    true
    true
    true
    true
    
    false
    false
    false
    false
    false
    false
    false
    false