php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43104 preg_* not capturing regex optional last parenthesis value if empty
Submitted: 2007-10-24 23:53 UTC Modified: 2008-01-16 17:37 UTC
From: ch+php at 1111-internet dot com Assigned:
Status: Closed Package: PCRE related
PHP Version: 4.4.7 OS: n/a
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ch+php at 1111-internet dot com
New email:
PHP Version: OS:

 

 [2007-10-24 23:53 UTC] ch+php at 1111-internet dot com
Description:
------------
The code and results attached show two examples of preg_* demonstrating how an optional parenthesis match at the end of a regular expression is not being captured if the optional condition is not matched.

I think this is a bug, because if the optional parenthesis is not the last one in the regex, the empty value IS captured - which suggests that it should not be omitted just because it happens to be the last one.


Reproduce code:
---------------
preg_match_all("/(1)(23)?/", "12314123", $match, PREG_SET_ORDER);
print_r($match);
print_r(preg_split("/(1)(23)?/", "12314123", -1, PREG_SPLIT_DELIM_CAPTURE));


Expected result:
----------------
Array
(
    [0] => Array
        (
            [0] => 123
            [1] => 1
            [2] => 23
        )

    [1] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 
        )

    [2] => Array
        (
            [0] => 123
            [1] => 1
            [2] => 23
        )

)
Array
(
    [0] => 
    [1] => 1
    [2] => 23
    [3] => 
    [4] => 1
    [5] => 
    [6] => 4
    [7] => 1
    [8] => 23
    [9] => 
)


Actual result:
--------------
Array
(
    [0] => Array
        (
            [0] => 123
            [1] => 1
            [2] => 23
        )

    [1] => Array
        (
            [0] => 1
            [1] => 1
        )

    [2] => Array
        (
            [0] => 123
            [1] => 1
            [2] => 23
        )

)
Array
(
    [0] => 
    [1] => 1
    [2] => 23
    [3] => 
    [4] => 1
    [5] => 4
    [6] => 1
    [7] => 23
    [8] => 
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-11-23 01:32 UTC] felipe@php.net
This is expected for flag different of default. (PREG_PATTERN_ORDER)
 [2008-01-13 15:05 UTC] nlopess@php.net
Yeah, it has been like this for ages, so we can't "fix" it. It will only add the optional captures if they are in the middle. In that case we add some empty padding values.
 [2008-01-16 17:37 UTC] ch+php at 1111-internet dot com
No problem - I understand why this shouldn't be changed, and in my particular case I was able to work around it by adding a non-optional match after the optional one. So bug closed.

Thanks to the entire PHP team for building and maintaining a great product!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 05:01:28 2024 UTC