还是没看明白问题...
能不能说的具体一点?你想用这个做什么应用?
举两个符合或者不符合的例子BTW:括号外面可以有逻辑操作符吗?

解决方案 »

  1.   

    这里不能用正则,因为respString是一堆字符串,不知道是什么,动态的.
    String respString = "This is respString.with this string you can....";
    String matchString = "(\"is\" or \"This\") and \"with\"";
    向上面这两个参数传进去就会返回true
    如果String matchString = "(\"is\" or \"This\") and \"withxxx\"";就会返回false
    我自已写了一段,只能是没括号且全部是and的情况可以实现.但有其他的就不行了.
    boolean recv (boolean result ,String respData,String recvString){
            // "na me" or "gender" and "birth"
            boolean rtn = result;
            String wait="";
            String waitStr = recvString;
            waitStr = waitStr.replaceAll("\"", "");
            String next = waitStr;        System.out.println("waitStr = " + waitStr);        while (next.length() > 3) {
                if (recvString.startsWith("\"(")) {
                    waitStr = recvString.substring(recvString.indexOf("\"(") + 2, recvString.indexOf(")\"") - 2);
                    recv(result, respData, waitStr);
                } else {
                    if (next.indexOf("and") >= 0) {
                        wait = next.substring(0, next.indexOf("and"));
                        System.out.println("wait = " + wait);
                        next = next.substring(next.indexOf("and") + 4);
                        System.out.println("next = " + next);
                        rtn = rtn && respData.indexOf(wait) >= 0;
                        System.out.println("rtn = " + rtn);
                        recv(rtn, respData, next);
                    }else{
                        wait = next;
                        rtn = rtn && respData.indexOf(wait)>=0;
                        break;
                    }            }
            }
            return rtn;
    }
      

  2.   

    为何不用正则?
    String respString = "This is respString.with this string you can....";
    这个动态很正常关键是
    String matchString = "(\"is\" or \"This\") and \"with\"";
    是动态的吗?如果是你可以动态生成表达式
    然后再动态比较
    这里要了解你这个 matchString 的生成方式
      

  3.   

    matchString 的生成方式 是从一个vector里取出来的 (String)vector.get(i);
    我觉得现在不是用不用正则的问题,是解决这个问题的思路.
      

  4.   

    思路不就是:
    有一个待比较的字符串 s1,和一个通配字符串 s2
    你要比较 s1 里面的内容是不是符合 s2 的通配条件你所说的思路是指什么?
      

  5.   

    FT
    正则不就是处理这些的吗
    你非要自己写一个类似正则的工具
    "(\"is\" or \"This\") and \"with\""
    完全可以用
    [(This)(is)].*with
    代替然后进行比较可以试试按照你的逻辑把通配表达式替换成正则然后再比较
      

  6.   

    又发现了你这个处理逻辑的一个漏洞
    如果有这么个通配串
    "(\"sword\" or \"andy\") and \"nor\""
    匹配这个
    "sword andy.with this nor you can...."
    你这个处理逻辑就彻底抓瞎了吧...
      

  7.   

    该题用正则表达式可以轻松实现。From JDK1.4  String.match(String pattern)支持正则表达式匹配。可参考
    http://community.csdn.net/Expert/topic/3204/3204159.xml?temp=.4580957更详细的可以看Pattern的Java Doc,他对如何写正则表达式有详细的说明。
      

  8.   

    String respString = "This is respString.with this string you can....";
    String matchString = "(\"is\" or \"aaa\") and \"with\"";是不是这样返回的也是true?
      

  9.   

    如果有(('a' or 'b') and 'c') and (('d' or 'f'))等等,
    如果要使用正则表达式,你要分析你的String,再转变为正则表达式的符号规则
    如果你真想了解如何分解这样的字符串,请看编释原理或数据结构
      

  10.   

    各位,看来你们还没理解题目的意思,我说简单点吧,现在要实现在某个长串里找出他们是否符合这样的条件.如:有A且有B,
    "A" and "B"
    还有下面几种,括号里最多有两个,只有一对括号,不能嵌套
    ("A" and "B") or "C"   
    "A" and "B" and "C" and "D"
    "A" or ("B" and "C")
    "A" or "B" or "C" and "D"
    用正则我考虑过,是不行的,这里的and or ,是逻辑关系,
      

  11.   

    正则里面是有逻辑关系的
    与是默认的 : A and B ----> [(AB)]
    | 是或 : A or B ----> [A|B] 或者 [AB]还有你这个括号的意义是什么?
    一个单词?一个词组?一个句子?("A" and "B") or "C"
    是什么意思? 这个字符串里面要么有 A 和 B,要么有 C?
    A和B之间有字符没有? AdsdsdaB 算不算?"A" and "B" and "C" and "D" 
    代表有 A 有 B 有 C 有 D? DBCA 算吗?"A" or "B" or "C" and "D"
    代表有 A 或有 B 或有 C 而且 D?
    正则这样表达: [ABC].*D你要是非要发明自己的一种语言首先要有一套严密的语言规范
    然后用堆栈做吧
      

  12.   

    括号的意义是尤先级.
    A B C简单说明一下,是键盘上的任何字符(一个或多个),可能有空格.
    A可能是 china / c h n / x#$%y?><z 但不会有"号,那是他们的分隔符
    如:"get config" and "done.\n" or ">>>>>>>>>>>>>done. "
    明白吗?