正则表达式 - 返回拆分匹配

时间:2015-09-23 10:26:01

标签: php regex preg-match-all

我有代码:

<?php

$pattern = '~(?(?=hello 2)(hello 2)|hello (1))~';


$subjects = [];
$subjects[] = <<<EOD
test hello 2 test
EOD;


$subjects[] = <<<EOD
test hello 1 test
EOD;


$result = preg_match_all($pattern, $subjects[0], $matches);
assert($matches[1][0] == 'hello 2');

$result = preg_match_all($pattern, $subjects[1], $matches);
assert($matches[1][0] == '1');

我希望在一个数组中包含所有匹配项 - 数组中的2个项目(输入字符串,第一个或第二个表达式的结果),但现在我得到3个数组项(输入字符串,结果,空)或(输入字符串,空,结果)。在var转储中它是:

实际状态:

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

我想:

array(2) {
  [0] =>
  array(1) {
    [0] =>
    string(7) "hello 2"
  }
  [1] =>
  array(1) {
    [0] =>
    string(7) "hello 2"
  }
}
array(2) {
  [0] =>
  array(1) {
    [0] =>
    string(7) "hello 1"
  }
  [1] =>
  array(1) {
    [0] =>
    string(1) "1"
  }
}

1 个答案:

答案 0 :(得分:1)

您需要将{em>分支重置与?|

一起使用
$pattern = '~(?|(?=hello 2)(hello 2)|hello (1))~';

请参阅IDEONE demo

这样,您将避免非参与组作为结果匹配数组的一部分出现。

有关详细信息,请参阅regular-expressions.info上的Branch Reset Groups