简体   繁体   中英

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). In var dump it is:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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