Well here's a way. First collectFind the positionsposition of allthe first occurrence of x
:
posexpr = Position[HoldForm[xHoldForm[x + 2 + 4 + x]x];
pos = Position[expr, x];x, -1, 1];
Then:
ReplacePart[HoldForm[x + 2 + 4 + x]ReplacePart[expr, First[pos]pos -> 4]
4 + 2 + 4 + x