Bug #50887 [Com]: preg_match , last optional sub-patterns ignored when empy

From: Date: Fri, 01 Jul 2011 11:11:00 +0000
Subject: Bug #50887 [Com]: preg_match , last optional sub-patterns ignored when empy
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-161454@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=50887&edit=1

 ID:                 50887
 Comment by:         arveen dot ponniah at loginbn dot ch
 Reported by:        harrrrpo at gmail dot com
 Summary:            preg_match , last optional sub-patterns ignored when
                     empy
 Status:             Wont fix
 Type:               Bug
 Package:            PCRE related
 Operating System:   Windows
 PHP Version:        5.3.1
 Block user comment: N
 Private report:     N

 New Comment:

hello i'm tamilboy


Previous Comments:
------------------------------------------------------------------------
[2011-06-12 01:52:52] cappuccino dot e dot cornetto at gmail dot com

I cannot imagine how fixing it would break anything older.

If I expect 3 submatches from my pattern, but I get 2, then I know (for the bug) 
that the missing submatch is the last one and it’s an empty string. So I add it 
myself to the submatches array. Would a programmer do anything different to fix this 
bug?

If the bug is fixed, it means that my old code will always get 3 submatches from 
that pattern. So my own fix won’t get triggered, and having the last submatch the 
same value (empty string) as the one my fix would have added, I won’t have any 
issue, except a bit of (stale) unused code.

------------------------------------------------------------------------
[2010-01-31 11:57:09] nlopess@php.net

I don't think we can change that behaviour at this point for the sake of not brekaing BC.

------------------------------------------------------------------------
[2010-01-30 16:58:58] harrrrpo at gmail dot com

Description:
------------
in preg_match , when optional sub-patterns (using ? or {0,n} ) are the last sub-patterns and empty
(e.g. not matched) they are ignored in $matches array
this behavior is inconsistent with preg_match_all , and with the case when the empty optional
sub-pattern isn't the last one

Reproduce code:
---------------
$str="1";
preg_match("#\d(\d)?#",$str,$mt);
var_dump($mt);

Expected result:
----------------
array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(0) ""
}

(the string(0) "" does appear on all cases with preg_match_all , and with preg_match ,
when there is any additional sub-patterns after it)

Actual result:
--------------
array(1) {
  [0]=>
  string(1) "1"
}

(the value of sub-pattern vanished)


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=50887&edit=1


Thread (9 messages)

« previous php.bugs (#161454) next »