Skip to main content
4 of 4
added 98 characters in body
Junho Lee
  • 5.2k
  • 1
  • 17
  • 34

Edit

For order preserving as Jens says, I changed Attributes

ClearAttributes[Plus, Orderless]

HoldForm[7 + x + 2 + 4 + x + 5] /. f___ + x + l___ :> f + 4 + l

7 + 4 + 2 + 4 + x + 5

And you can revert by SetAttributes[Plus, Orderless]


Origin

How about this

HoldForm[x + 2 + 4 + x] /. x + a___ -> 4 + a

4 + 2 + 4 + x

Junho Lee
  • 5.2k
  • 1
  • 17
  • 34