说明:
分隔符 (),/,//(a) b :  b,a b 均正确.括号里的词可有可无
(a/b) c :  c,a c,b c 均正确./前后的词是或的关系
a//b : a,b均正确要求:
把以上符号表示的词语组合转换成正则表示法
例:((it is/’s) the/a) cat // (It is/’s) (the) animal

解决方案 »

  1.   


    不太理解你要表达的意思首先(a)b匹配的是字符“ab”,子字符串“a”
    其次(a/b)c匹配的是字符“a/bc”,子字符串“a/b”
    再次a//b匹配的是字符串“a//b”((it is/’s) the/a) cat // (It is/’s) (the) animal你是想写正则表达式匹配
    it is a cat
    it is the cat
    it's a cat
    it's the cat
    it is animal
    it is the animal
    it's animal
    it's the animal
    吧?(it('s|\s+is)\s+(a|the)\s+cat)|(it('s|\s+is)\s+(animal|the\s+animal))
      

  2.   

    除了上面这些,
    cat
    a cat
    the cat
    animal
    the animal
    都是对的。
    我要一种转化方式,分隔符 (),/,// 这些是约定规则,并非正则语意.感谢1楼回复
      

  3.   

    做个事例便于理解function pregRule($str){
        $str = trim($str);
        return '/^'.str_replace(array(') ','[',']','//','/',' '),array(')?\s*','(','){1}','|','|','\s+'),$str).'$/'; 
    }$str = "(a) b";
    echo pregRule($str).'<br>';
    $str = "[a//b]";        #加个中括号,方便转换
    echo pregRule($str).'<br>';
    $str = "(a/b) c";
    echo pregRule($str).'<br>';
    $str = "((it [is/’s]) the/a) cat // (It [is/’s]) (the) animal";
    echo pregRule($str).'<br>';
    #result:
    /^(a)?\s*b$/
    /^(a|b){1}$/
    /^(a|b)?\s*c$/
    /^((it\s+(is|’s){1})?\s*the|a)?\s*cat\s+|\s+(It\s+(is|’s){1})?\s*(the)?\s*animal$/前三种还凑合,第四个匹配就不忍心看了,请大家帮忙丰富这个函数,使之适应,可以适当修改语句
      

  4.   


    第四个主要的问题在于把“/”换成“|”a/b
    ca/bd
    第一种情况下不需要考虑太多  直接把“/”换成“|”就ok
    第二种情况下,就有要考虑的了。你是想得到cad,cbd还是想得到ca,bd呢?所以这个时候就不能只考虑“/”换成“|”下面是你的第四个替换之后的表达式
    /^((it\s+(is|’s){1})?\s*the|a)?\s*cat\s+|\s+(It\s+(is|’s){1})?\s*(the)?\s*animal$/
    只看前半部分
    ((it\s+(is|’s){1})?\s*the|a)?\s*cat\s+ 中的 
    (it\s+(is|’s){1})?\s*the|a)
    你想得到it\s+(is|’s){1})?\s*匹配的字符然后拼接the或者a
    但实际上表达式呈现出来的是你想得到it\s+(is|’s){1})?\s*the或者a你想要 i want(the|a),但实际上你却说 i want the|a