Req #77756 [Opn->Wfx]: PCRE2 Replacement String Conditional

From: Date: Sun, 17 Mar 2019 07:12:03 +0000
Subject: Req #77756 [Opn->Wfx]: PCRE2 Replacement String Conditional
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220013@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77756&edit=1 ID: 77756 Updated by: requinix@php.net Reported by: fenniclog at gmail dot com Summary: PCRE2 Replacement String Conditional -Status: Open +Status: Wont fix Type: Feature/Change Request Package: PCRE related PHP Version: 7.3.3 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2019-03-17 02:02:32] 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) { } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77756&edit=1

« previous php.bugs (#220013) next »