|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2019-03-17 07:12 UTC] requinix@php.net
-Status: Open
+Status: Wont fix
[2019-03-17 07:12 UTC] requinix@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 08:00:01 2025 UTC |
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) { }