-1

I referred this question - Capture group reference + numeral

I tried with both the options provided there. Here is example code using ECMAScript expression (I guess I need to use this, correct me if I am wrong) -

$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);

Expected output

box.last_rollout_revision = 234

Actual getting

34

P.S. I am trying to replace value against a key in a config file which has several other key-value pairs. I think regex will make it easier in this case. Correct me if I am wrong.

Community
  • 1
  • 1
Sandeepan Nath
  • 8,771
  • 17
  • 66
  • 131

1 Answers1

0

Change the replacement pattern to

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

See the IDEONE demo

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397