php.net |  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
Votes:1
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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: fenniclog at gmail dot com
New email:
PHP Version: OS:

 

 [2019-03-17 02:02 UTC] fenniclog at gmail dot com
Description:
------------
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}.
Ref: https://www.regular-expressions.info/replaceconditional.html#pcre2

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:
---------------
<?php
$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
bool(true)

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
bool(false)
array(0) {
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-17 07:12 UTC] requinix@php.net
-Status: Open +Status: Wont fix
 [2019-03-17 07:12 UTC] requinix@php.net
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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC