$s="aaa:  b bb  c cc:  ddd  eee  ffff"
我想用正则表达式匹配aaa:与ddd之间的内容,要求开头和结尾没有空格
我写的是(?<=(aaa:\s*)).*(?=\s*ddd)
执行失败, 请帮我改改.

解决方案 »

  1.   

    php 的正则断言不能是可变长度的$s="aaa:  b bb  c cc:  ddd  eee  ffff";
    preg_match_all('/(?<=aaa:)\s*(.*)(?=\s*ddd)/', $s, $r);
    print_r($r);Array
    (
        [0] => Array
            (
                [0] =>   b bb  c cc:  
            )    [1] => Array
            (
                [0] => b bb  c cc:  
            ))
      

  2.   


    <?php$s="aaa:  b bb  c cc:  ddd  eee  ffff";$reg = "/aaa:\s*(.*?)\s+ddd/";preg_match($reg, $s, $matches);
    print_r($matches);
      

  3.   

    我说了不算,手册说
    Lookbehind assertions start with (?<= for positive assertions and (?<! for negative assertions. For example, (?<!foo)bar does find an occurrence of "bar" that is not preceded by "foo". The contents of a lookbehind assertion are restricted such that all the strings it matches must have a fixed length. However, if there are several alternatives, they do not all have to have the same fixed length. Thus (?<=bullock|donkey) is permitted, but (?<!dogs?|cats?) causes an error at compile time. Branches that match different length strings are permitted only at the top level of a lookbehind assertion. This is an extension compared with Perl 5.005, which requires all branches to match the same length of string. An assertion such as (?<=ab(c|de)) is not permitted, because its single top-level branch can match two different lengths, but it is acceptable if rewritten to use two top-level branches: (?<=abc|abde) The implementation of lookbehind assertions is, for each alternative, to temporarily move the current position back by the fixed width and then try to match. If there are insufficient characters before the current position, the match is deemed to fail. Lookbehinds in conjunction with once-only subpatterns can be particularly useful for matching at the ends of strings; an example is given at the end of the section on once-only subpatterns. 向后断言的限制,使得它匹配的所有字符串必须有一个固定的长度,但是,如果有多个选择,但不是所有的具有相同的固定长度,因此允许(?<=bullock|donkey)