|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #77756 PCRE2 Replacement String Conditional
Submitted: 2019-03-17 02:02 UTC Modified: 2019-03-17 07:12 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: fenniclog at gmail dot com Assigned:
Status: Wont fix Package: PCRE related
PHP Version: 7.3.3 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.
Block user comment
Status: Assign to:
Bug Type:
From: fenniclog at gmail dot com
New email:
PHP Version: OS:


 [2019-03-17 02:02 UTC] fenniclog at gmail dot com
When working on a chat command system for a project, I was reading up on PCRE to properly build a robust command match regex. I read up that PCRE2 supports Replacement String Conditional be using ${1:+matched:unmatched}.

I checked if PHP supported PCRE2 where I found a merged RFC PHP 7.3 about the topic. After testing my method, the conditional was not being parsed.

Test script:
$message = "ban bob 24 days";
$regex = '/\{((?:(?!\d)\w)+?):?(?:(?<=\:)([[:graph:]]+))?\}/';
$commandPattern = "ban {name:\w+} {duration}";

$pattern = str_replace('/', '\/', $commandPattern);
$regex = '/^'.preg_replace($regex, '(?<$1>${2:-.*})', $pattern).' ?/miu';
echo $regex.PHP_EOL;

$regexMatched = (bool)preg_match($regex, $message, $matches);
var_dump($regexMatched, $matches);

Expected result:
/^ban (?<name>\w+) (?<duration>.*) ?/miu

array(5) { 
    [0]=> string(15) "ban bob 24 days" 
    ["name"]=> string(3) "bob" 
    [1]=> string(3) "bob" 
    ["duration"]=> string(7) "24 days" 
    [2]=> string(7) "24 days"

Actual result:
/^ban (?<name>${2:-.*}) (?<duration>${2:-.*}) ?/miu
array(0) {


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-17 07:12 UTC]
-Status: Open +Status: Wont fix
 [2019-03-17 07:12 UTC]
PHP does not use pcre2_substitute, which is what provides that functionality.

I don't see a need to refactor ext/pcre to support this when preg_replace_callback() exists.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Apr 11 16:01:23 2021 UTC