# Replace x, but not x[a] [duplicate]

This question already has an answer here:

I want to replace all occurences of the symbol x with x[a], as long as it is not in the form x[a].

In other words, I am looking for magicReplacementRule in

x[a] + x /. magicReplacementRule
--> x[a] + x[a]


I tried

x[a] + x /. Except[x[a], x] -> x[a]


but the result was x[a][a] + x[a].

## marked as duplicate by Mr.Wizard♦Jun 4 '15 at 14:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

• Replace[x[a] + x, x -> y, {1}]? – kglr Nov 12 '14 at 21:07
• ... also x[a] + x /. {x -> y, z : _[_] :> z}? – kglr Nov 12 '14 at 21:13
• How about x[a] + x /. {x[a] -> x[a], x -> x[a]} – Chip Hurst Nov 12 '14 at 21:23
• @ChipHurst: Yes, that of course works. I feel sufficiently foolish now. (I think I had tried {x[a] -> x, x -> x[a]}, which was nonsense.) – Martin J.H. Nov 12 '14 at 21:29

## 3 Answers

Use Replace instead of ReplaceAll with the option Heads -> False.

Replace[x[a] + x, x -> y, {0, Infinity}, Heads -> False]


{0, Infinity} here is a level specification which tells Replace to replace everywhere, just like ReplaceAll. You can drop Heads -> False because it's the default setting for Replace, but I wanted to point out the option which controls this behaviour.

Update: It appears that since version 10, All can be used as a substitute for the {0, Infinity} level specification:

Replace[x[a] + x, x -> y, All]

• You can use -1 instead of Infinity in lvl specs, especially if you're concerned about character count. – rcollyer Nov 12 '14 at 22:05
• @rcollyer When I read this, I thought: wouldn't it be easier to just have a level specification All that stands for {0, Infinity}? It turns out it works, but I don't see it documented anywhere. – Szabolcs Nov 12 '14 at 22:26
• You might be right. I'll hunt for it, too. – rcollyer Nov 12 '14 at 22:59
• @rcollyer Just tried in 8 and 9. It seems this is new in v10. Only tested it in Level and Replace. Perhaps it doesn't yet work in all function that take level specifications. – Szabolcs Nov 12 '14 at 23:24

In the depth-first preorder traversal of ReplaceAll complete expressions are matched before heads therefore one can use a skip rule(1)(2). Since you also want to replace x with x[a], the pattern to be skipped, you can use a single rule with Alternatives:

{x[a], x} /. x[a] | x -> x[a]

{x[a], x[a]}


( Since Plus[x[a], x[a]] evaluates to 2 x[a] I used List in the example above. )

• I often use expr /. {pat -> pat, old -> new} -- explicitly here expr /. {x[a] -> x[a], x-> x[a]} -- but Alternatives works here since pat and new are the same. +1 – Michael E2 Jun 4 '15 at 3:00
• @MichaelE2 Do you have an answer where this "skip rule" features prominently? I'll add it to my superscript links above. – Mr.Wizard Jun 4 '15 at 3:24
• Sorry, nothing turns up. I think I've used it on this site. Unfortunately, Cases[SemanticImport["MSE", "CodeBlock"], {p_ -> p_, __}, Infinity] does not do what I want. :) – Michael E2 Jun 4 '15 at 3:45
• @Michael While looking for another example I realized this question is a duplicate. :-/ – Mr.Wizard Jun 4 '15 at 14:44

because the x you want to replace is in the last level of the expression you can use:

Replace[Sin[x] + x[a] + x, x -> y, {-1}]
(*y + Sin[y] + x[a]*)