I have to agree with halirutan that doing this with a single rule is not a great idea, because the result is likely to be gross and confusing. However, in order to prove that it's on the yucky side, I'm going to show the best way I could figure out how to do it. I make no promises about its robustness, but it at least seems to handle the most obvious wrinkles OK.
First, I match on heads applied to f
, and use Alternatives
to get either a Derivative
of f
or f
itself. Once you have that match with Alternatives
, you can do almost anything with it on the right-hand side. I don't think any of the choices are aesthetically pleasing, but here's one that I think is awful in a kind of fun way.
In[1]:= rule = h : (_Derivative[f] | f)[x] :>
FirstCase[h, _Derivative, Identity, {2}, Heads -> True]@y;
In[2]:= 2*f[x] + g'[z]*f''[x] /. rule
Out[2]= 2 y + y'' g'[z]
f'[x] + f[x] == 0 /. {f[___] -> y, Derivative[n__][f][___] :> Derivative[n][y]}
$\endgroup$