0

例:

$match_check = "(?'txt'(?<=:txt{)([^}]+)(?=}))|(?'reg'(?<=:reg{)([^}]+)(?=}))";

$route_from_value = ':txt{resultxt}:txt{test}:reg{/^[a-zA-Z]*$/}:reg{regexresult}';

preg_match_all('/'.$match_check.'/', $route_from_value, $get_matchers_check);

var_dump($get_matchers_check);

与えられた質問の結果は次のとおりです。

array(7) {
  [0] =>
  array(4) {
    [0] =>
    string(8) "resultxt"
    [1] =>
    string(4) "test"
    [2] =>
    string(13) "/^[a-zA-Z]*$/"
    [3] =>
    string(11) "regexresult"
  }
  'txt' =>
  array(4) {
    [0] =>
    string(8) "resultxt"
    [1] =>
    string(4) "test"
    [2] =>
    string(0) ""
    [3] =>
    string(0) ""
  }
  [1] =>
  array(4) {
    [0] =>
    string(8) "resultxt"
    [1] =>
    string(4) "test"
    [2] =>
    string(0) ""
    [3] =>
    string(0) ""
  }
  [2] =>
  array(4) {
    [0] =>
    string(8) "resultxt"
    [1] =>
    string(4) "test"
    [2] =>
    string(0) ""
    [3] =>
    string(0) ""
  }
  'reg' =>
  array(4) {
    [0] =>
    string(0) ""
    [1] =>
    string(0) ""
    [2] =>
    string(13) "/^[a-zA-Z]*$/"
    [3] =>
    string(11) "regexresult"
  }
  [3] =>
  array(4) {
    [0] =>
    string(0) ""
    [1] =>
    string(0) ""
    [2] =>
    string(13) "/^[a-zA-Z]*$/"
    [3] =>
    string(11) "regexresult"
  }
  [4] =>
  array(4) {
    [0] =>
    string(0) ""
    [1] =>
    string(0) ""
    [2] =>
    string(13) "/^[a-zA-Z]*$/"
    [3] =>
    string(11) "regexresult"
  }
}

しかし、期待される結果は(正規表現だけでどうやって作るのですか?)または単純なものでなければなりません:

 'txt' =>
  array(4) {
    [0] =>
    string(8) "resultxt"
  },
 'txt' =>
  array(4) {
    [0] =>
    string(8) "resultxt"
  }
 'reg' =>
  array(4) {
    [0] =>
    string(8) "/^[a-zA-Z]*$/"
  }
 'reg' =>
  array(4) {
    [0] =>
    string(8) "regexresult"
  }
4

1 に答える 1