Bug #75181 [NEW]: PHP's PCRE API doesn't differentiate empty matches from non-matches

From: Date: Mon, 11 Sep 2017 05:23:32 +0000
Subject: Bug #75181 [NEW]: PHP's PCRE API doesn't differentiate empty matches from non-matches
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-211053@lists.php.net to get a copy of this message
From:             jocrutrisi at ibsats dot com
Operating system: All
PHP version:      7.2.0RC1
Package:          PCRE related
Bug Type:         Bug
Bug description:PHP's PCRE API doesn't differentiate empty matches from non-matches

Description:
------------
PCRE differentiates "a sub-pattern that matched and is empty" from "a
sub-pattern that didn't match". But in PHP's exposed APIs both of these
cases produce a sub-pattern key with an empty match.

Probably this can be a flag, for unfortunate B.C. reasons, but the
current behavior is certainly not correct.

Test script:
---------------
$re = '%^
    ((?=x))?
    ((?=y))?
    (\w)
$%mx';

$str = 'x
y
z';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

Expected result:
----------------
Observe the identical PCRE regex and input on Regex101, notice the flag
"isParticipating" clearly points out which pattern matched (even if
empty) or didn't match:

[
  [
    {
      "content": "x",
      "isParticipating": true,
      "groupNum": 0,
    },
    {
      "content": "",
      "isParticipating": true,
      "groupNum": 1,
    },
    {
      "content": "",
      "isParticipating": false,
      "groupNum": 2,
    },
    {
      "content": "x",
      "isParticipating": true,
      "groupNum": 3,
    }
  ],
  [
    {
      "content": "y",
      "isParticipating": true,
      "groupNum": 0,
    },
    {
      "content": "",
      "isParticipating": false,
      "groupNum": 1,
    },
    {
      "content": "",
      "isParticipating": true,
      "groupNum": 2,
    },
    {
      "content": "y",
      "isParticipating": true,
      "groupNum": 3,
    }
  ],
  [
    {
      "content": "z",
      "isParticipating": true,
      "groupNum": 0,
    },
    {
      "content": "",
      "isParticipating": false,
      "groupNum": 1,
    },
    {
      "content": "",
      "isParticipating": false,
      "groupNum": 2,
    },
    {
      "content": "z",
      "isParticipating": true,
      "groupNum": 3,
    }
  ]
]




Source: https://regex101.com/r/N90TJE/1

Actual result:
--------------
Both matching-and-empty and non-matching are just empty strings, we
can't tell them apart:

array(3) {
  [0]=>
  array(4) {
    [0]=>
    string(1) "x"
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
    [3]=>
    string(1) "x"
  }
  [1]=>
  array(4) {
    [0]=>
    string(1) "y"
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
    [3]=>
    string(1) "y"
  }
  [2]=>
  array(4) {
    [0]=>
    string(1) "z"
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
    [3]=>
    string(1) "z"
  }
}

-- 
Edit bug report at https://bugs.php.net/bug.php?id=75181&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=75181&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=75181&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=75181&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=75181&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=75181&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=75181&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=75181&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=75181&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=75181&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=75181&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=75181&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=75181&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=75181&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75181&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=75181&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=75181&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=75181&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75181&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=75181&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=75181&r=mysqlcfg



Thread (5 messages)

« previous php.bugs (#211053) next »