简体   繁体   English

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

[英]Regular expression - return split matches

I have the code: 我有代码:

<?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');

I want have all matches in one array - 2 items in array (input string, result from first or second expression), but now I get 3 items of array (input string, result, empty) or (input string, empty, result). 我想在一个数组中有所有匹配项-数组中有2个项目(输入字符串,第一个或第二个表达式的结果),但是现在我得到了3个数组中的项目(输入字符串,结果,空)或(输入字符串,空,结果) 。 In var dump it is: 在var dump中,它是:

Actual state: 实际状态:

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

I want: 我想要:

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

You need to use a branch reset with ?| 您需要对?|使用分支重置 ?| :

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

See IDEONE demo IDEONE演示

With this, you will avoid non-participating groups to appear as part of the resulting match array. 这样,您将避免非参与组出现在结果匹配数组中。

See Branch Reset Groups at regular-expressions.info for more details. 有关更多详细信息,请参见regular-expressions.info的分支重置组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM