fork download
  1. <?php
  2.  
  3. $re = '/(?|\/{(\w+)\?}|{(\w+)})/';
  4. $str = '/foo/{bar}/{baz?}';
  5. preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
  6. print_r($matches);
Success #stdin #stdout 0.02s 82944KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [0] => {bar}
            [1] => bar
        )

    [1] => Array
        (
            [0] => /{baz?}
            [1] => baz
        )

)