PHP的捕获组+数字

时间:2016-05-06 11:51:13

标签: php regex capture-group

我提到了这个问题 - Capture group reference + numeral

我尝试了两种选项。这是使用ECMAScript表达式的示例代码(我想我需要使用它,如果我错了就纠正我) -

$string = "box.last_rollout_revision = dummy";

$regex = "/(box\.last_rollout_revision[\s]*=[\s]*)[a-z0-9-_]*([\n]*)/";

$test = "234";
$replacementPattern = "$1".$test."$2";

echo preg_replace($regex,$replacementPattern,$string);

预期输出

box.last_rollout_revision = 234

实际获取

34

P.S。我试图将值替换为配置文件中的键,该配置文件具有其他几个键值对。我认为正则表达式会使这种情况更容易。如果我错了,请纠正我。

1 个答案:

答案 0 :(得分:0)

将替换模式更改为

$replacementPattern = '${1}'.$test.'$2';

请参阅IDEONE demo

相关问题