`regex{n,}?` == `regex{n}`?

后端 未结 7 1851
灰色年华
灰色年华 2021-01-20 15:06

-edit- NOTE the ? at the end of .{2,}?

I found out you can write

.{2,}?

Isnt that exactly the same as bel

7条回答
  •  梦毁少年i
    2021-01-20 15:18

    Not exactly Using PHP to do a regexp match and display the capture

    $string = 'aaabbaabbbaaa';
    
    $search = preg_match_all('/b{2}a/',$string,$matches,PREG_SET_ORDER );
    
    echo '
    ';
    var_dump($matches);
    echo '
    '; $search = preg_match_all('/b{2,}?a/',$string,$matches,PREG_SET_ORDER ); echo '
    ';
    var_dump($matches);
    echo '
    ';

    First result gives:

    array(2) {
      [0]=>
      array(1) {
        [0]=>
        string(3) "bba"
      }
      [1]=>
      array(1) {
        [0]=>
        string(3) "bba"
      }
    }
    

    second gives:

    array(2) {
      [0]=>
      array(1) {
        [0]=>
        string(3) "bba"
      }
      [1]=>
      array(1) {
        [0]=>
        string(4) "bbba"
      }
    }
    

    With b{2} the capture only returns 2 b's, with b{2,} it returns 2 or more

提交回复
热议问题