0

Example:

$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);

And result for given question are:

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"
  }
}

But, expected result should be (how make it only with regexp?) or something simply:

 '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

0

preg_match_alland函数的输出preg_split遵循与您想要的不匹配的格式。

您可以做的只是对 的输出进行一些后处理preg_match_all以获得结果(类似于一['key', 'value']对数组)。请注意,您应该设置标志PREG_OFFSET_CAPTURE以获取匹配的索引以进行比较。不匹配的捕获组将提供索引-1不会返回包含 2 个元素的数组。

顺便说一句,您可以从您的正则表达式中删除一些不必要的捕获组。

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

样品运行:

$matches = null;
$returnValue = preg_match_all('/(?\'txt\'(?<=:txt{)[^}]+(?=}))|(?\'reg\'(?<=:reg{)[^}]+(?=}))/', ':txt{resultxt}:txt{test}:reg{/^[a-zA-Z]*$/}:reg{regexresult}', $matches, PREG_OFFSET_CAPTURE);

输出:

array (
  0 => 
  array (
    0 => 
    array (
      0 => 'resultxt',
      1 => 5,
    ),
    1 => 
    array (
      0 => 'test',
      1 => 19,
    ),
    2 => 
    array (
      0 => '/^[a-zA-Z]*$/',
      1 => 29,
    ),
    3 => 
    array (
      0 => 'regexresult',
      1 => 48,
    ),
  ),
  'txt' => 
  array (
    0 => 
    array (
      0 => 'resultxt',
      1 => 5,
    ),
    1 => 
    array (
      0 => 'test',
      1 => 19,
    ),
    2 => // No match found, index -1
    array (
      0 => '',
      1 => -1,
    ),
    3 => // No match found, index -1
    array (
      0 => '',
      1 => -1,
    ),
  ),
  1 => 
  array (
    0 => 
    array (
      0 => 'resultxt',
      1 => 5,
    ),
    1 => 
    array (
      0 => 'test',
      1 => 19,
    ),
    2 => 
    array (
      0 => '',
      1 => -1,
    ),
    3 => 
    array (
      0 => '',
      1 => -1,
    ),
  ),
  'reg' => 
  array (
    0 => '', // No match found, not array of 2 elements [<matched text>, <index>]
    1 => '', // No match found, not array of 2 elements [<matched text>, <index>]
    2 => 
    array (
      0 => '/^[a-zA-Z]*$/',
      1 => 29,
    ),
    3 => 
    array (
      0 => 'regexresult',
      1 => 48,
    ),
  ),
  2 => 
  array (
    0 => '',
    1 => '',
    2 => 
    array (
      0 => '/^[a-zA-Z]*$/',
      1 => 29,
    ),
    3 => 
    array (
      0 => 'regexresult',
      1 => 48,
    ),
  ),
)
于 2013-09-14T23:35:54.627 回答