php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79028 Several same-name subpatterns in distinct piped alternatives should be allowed
Submitted: 2019-12-24 17:18 UTC Modified: 2019-12-25 10:05 UTC
From: php4fan at gmail dot com Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: 7.2.26 OS:
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: php4fan at gmail dot com
New email:
PHP Version: OS:

 

 [2019-12-24 17:18 UTC] php4fan at gmail dot com
Description:
------------
see below test script and expected and observed behavior below.

Maybe this an issue at the level of regular expressions specifications. As I only use them in PHP, I don't know, so I report it here. Also, it's so evidently flawed that maybe PHP could implement them correctly anyway while waiting for the specs to be fixed; not sure about that.

Test script:
---------------
<?php

$content = <<<'HERE'

First match: foo::"XYZ"
Second match: bar--<XYZ>

HERE;


$content=preg_replace_callback('#foo::"(?<sub>[^"]*)"|bar--<(?<sub>[^>]*)>#', 'testReplace', $content);

function testReplace($match) {
    
    print_r($match);
    return 'XXX';
}

Expected result:
----------------
Array
(
    [0] => foo::"XYZ"
    [sub] => XYZ
    [1] => XYZ
)
Array
(
    [0] => bar--<XYZ>
    [1] => 
    [sub] => XYZ
    [2] => XYZ
)


Actual result:
--------------
Warning: preg_replace_callback(): Compilation failed: two named subpatterns have the same name


Because the subgroups with the same name appear on two sides of a "|", there should be no conflict. At most one of the piped patterns (i.e. either side of the "|") can match, they are mutually exclusive, so there is no ambiguity possible and it should be a problem that a subgroup appear in both of them with the same name, as it can only be assigned once anyway.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-24 17:36 UTC] php4fan at gmail dot com
-Status: Open +Status: Closed
 [2019-12-24 17:36 UTC] php4fan at gmail dot com
Ok, I see, "(?J)" modifies the behavior to accomplish what I asked for.

So, not a bug.

Sorry about the noise.
 [2019-12-25 10:05 UTC] cmb@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Dec 03 06:03:43 2021 UTC