# Extracting argument of a specific functions in a large expression

I have a large expression, for example

Cos[x]Sin[y]Sqrt[1+z]/(1+x^2)-1/(1+y)


I wish to extract the argument inside Sqrt function, namely 1+z. I thought of using /.Sqrt[x_]->(h=x), but executing

Cos[x]Sin[y]Sqrt[1+z]/(1+x^2)-1/(1+y)/.Sqrt[x_]->(h=x);h


returns x but not 1+z, why? Is there a way to achieve my goal?

In my expressions, there will only be one Sqrt, but many other different heads as well. There is a similar question, but it is only applicable to very simple expression like Sqrt[1+z].

• You did not get what you expected because you used Rule(->) rather than RuleDelayed(:>) – Bob Hanlon Mar 7 at 20:23
• @BobHanlon I see, thank you very much! – pisco Mar 10 at 10:39

Clear["Global*"]

expr = Cos[x] Sin[y] Sqrt[1 + z]/(1 + x^2) - 1/(1 + y) Sqrt[1 - z];

Cases[expr, Sqrt[t_] :> t, Infinity]

(* {1 - z, 1 + z} *)


To ensure that all of the terms are real

And @@ Thread[% >= 0] // Simplify

(* -1 <= z <= 1 *)


Or more directly,

And @@ Cases[expr, Sqrt[t_] :> (t >= 0), Infinity] // Simplify

(* -1 <= z <= 1 *)


Better yet,

FunctionDomain[expr, z]

(* -1 <= z <= 1 *)
`