Case #1
Observe:
"anything" /. Plus[___] -> "match"
"match"
This is because Plus[___]
evaluates to ___
, and ___
matches anything. You can use HoldPattern
:
Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]
For this particular case you could also use the form _Plus
, which matches any expression with head Plus
but not Plus
itself:
Sqrt[Plus[x, y]] /. _Plus -> u
Sqrt[u]
Case #2
You must understand that pattern matching is done on something close to the FullForm
of an expression, rather than the StandardForm
output you normally see. Let's look at your second problematic expression:
a/Sqrt[x] // FullForm
Times[a,Power[x,Rational[-1,2]]]
As you can see, Sqrt
is nowhere to be found.
You could prevent the evaluation of Sqrt
on both the left-hand and right-hand sides:
Unevaluated[a/Sqrt[x]] /. HoldPattern[Sqrt[_]] -> u
a/u
If you need to match for things that appear as Radicals in StandardForm you can use the methods described herehere. An application of that might look like this:
ToBoxes[a/Sqrt[x]] /. _SqrtBox -> u // ToExpression
a/u