#44463 [Bgs]: preg_match_all makes extra matches on empty alternatives

From: Date: Mon, 17 Mar 2008 23:25:40 +0000
Subject: #44463 [Bgs]: preg_match_all makes extra matches on empty alternatives
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-123406@lists.php.net to get a copy of this message
ID: 44463 User updated by: php at sameprecision dot org Reported By: php at sameprecision dot org Status: Bogus Bug Type: PCRE related Operating System: irrelevant PHP Version: 5.2.5 New Comment: I am querying PCRE developers about it... A global search SHOULD end if the last match matched a end of subject assertion... What is really happening is that at the end of the subject string, a global search performs another test--if the regex matches empty string, it produces this additional match... That is ambiguous. Previous Comments: ------------------------------------------------------------------------ [2008-03-17 23:19:22] felipe@php.net Well, at least isn't a PHP bug. However, seems to me that the $ is matching twice... File a bug in the PCRE bug repository: http://bugs.exim.org/enter_bug.cgi?product=PCRE ------------------------------------------------------------------------ [2008-03-17 22:49:25] php at sameprecision dot org Why would you expect three matches for a,a ? ------------------------------------------------------------------------ [2008-03-17 22:41:00] felipe@php.net This result is expected. Using pcretest: PCRE version 7.5-RC2 2007-12-27 re> /(a|)(,|$)/g data> a, 0: a, 1: a 2: , 0: 1: 2: data> a,a 0: a, 1: a 2: , 0: a 1: a 2: 0: 1: 2: Thanks. ------------------------------------------------------------------------ [2008-03-17 22:13:54] php at sameprecision dot org Description: ------------ Performing a global match with empty alternative in subpattern followed by $ alternative in subpattern results in "extra" match. In the example below, I believe that preg_match_all should match the second 'a' in the first subpattern, match the end of subject assertion in the second subpattern and flag the global search as over. I've tried this with php5.2.5/pcre7.3 on linux. Reproduce code: --------------- $regex = '/(a|)(,|$)/'; echo preg_match_all($regex, 'a,', $m1, PREG_SET_ORDER)."\n"; print_r($m1); echo preg_match_all($regex, 'a,a' ,$m2, PREG_SET_ORDER)."\n"; print_r($m2); Expected result: ---------------- 2 Array ( [0] => Array ( [0] => a, [1] => a [2] => , ) [1] => Array ( [0] => [1] => [2] => ) ) 2 Array ( [0] => Array ( [0] => a, [1] => a [2] => , ) [1] => Array ( [0] => a [1] => a [2] => ) ) Actual result: -------------- 2 Array ( [0] => Array ( [0] => a, [1] => a [2] => , ) [1] => Array ( [0] => [1] => [2] => ) ) 3 Array ( [0] => Array ( [0] => a, [1] => a [2] => , ) [1] => Array ( [0] => a [1] => a [2] => ) [2] => Array ( [0] => [1] => [2] => ) ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=44463&edit=1

« previous php.bugs (#123406) next »