# Replace sub-expressions matching a pattern unless located under specific heads

Suppose I have some rule, e.g. Re[x_] :> (x + Conjugate[x])/2. I want to apply it to all matching sub-expressions in some expression, excluding any sub-expressions that are located somewhere under heads Hold[…] or Function[…] (at any depth). What is the simplest way to do that? Can it be done without explicit filtering of positions?

For example, the result of replacement in the expression:

{z + Re[a], Hold[1 + Re[b]], Function[z, Abs[Re[c]]]}


should be

{z + (a + Conjugate[a])/2, Hold[1 + Re[b]], Function[z, Abs[Re[c]]]}


where Re[b] and Re[c] are left unmodified, because they are located under Hold and Function.

• Vladimir, an example of some expression would make it so much easier to answer your question. – kglr Sep 1 '19 at 1:44
• It seems that my question is a duplicate of stackoverflow.com/q/6451802/305118 – Vladimir Reshetnikov Sep 1 '19 at 2:43

ReplaceAll[