# Replace something to an expression

After some calculations, I get some long expressions. I want to change some variables to get simple looks.

For a just small example: But it doesn't work.

I convert the expression to RawInputForm in order to ask here. So, it partially works: CODES:

 expr=(2*Sqrt*E^(I*(q*y + (5*x*(a - Sqrt[a^2 + 6*a]))/(3*\[Alpha]*y) + (7*x*(a - Sqrt[a^2 + 6*a]))/(3*\[Alpha]*t)))*Sqrt[\[Alpha]]*\[Delta])/(Sqrt[\[Beta] + 1]*(\[Eta]/E^(\[Delta]*(x + y)) + 4*(E^(\[Delta]*x))^2))


Transforms:

transforms = {(2*Sqrt*Sqrt[\[Alpha]]*\[Delta])/Sqrt[\[Beta] + 1] -> Subscript[C, 1], (x*(a - Sqrt[a^2 + 6*a]))/(3*\[Alpha]) -> Subscript[C, 2]}

expr/.transforms


Replacements acts on the full form of an expression. Look e.g. at:  And the expression in your replacement is:

(x*(a - Sqrt[a^2 + 6*a]))/(\[Alpha]) // FullForm You see that Rational[7, 3] and Power[t,-1] are the reason that the 2 expressions do not match. you may at most match the term in brackets.

Further, the whole expression reads Times[...] and the first replacement changes some terms in Times[...]. MMA then thinks it is done with everything inside Times[...]. Therefore, to make the second replacement, you need to apply ReplaceAll again or simpler, use ReplaceRepeated (//.):

transforms = {(2*Sqrt*Sqrt[\[Alpha]]*\[Delta])/Sqrt[\[Beta] + 1] ->
Subscript[C, 1], (x*(a - Sqrt[a^2 + 6*a])) -> Subscript[C, 2]}

expr //. transforms  // Simplify 1. Slightly change your second rule:
rules = {(2 Sqrt Sqrt[α] δ)/Sqrt[1 + β] -> Subscript[C, 1],
a - Sqrt[6 a + a^2] -> 3 α Subscript[C, 2] / x} 1. Apply the replacement rules sequentially using Fold:
Fold[ReplaceAll, expr, rules] Alternatively, use ReplaceRepeated as in Daniel Huber's answer:

expr //. rules

same result Clear["Global*"]

expr = (2*Sqrt*
E^(I*(q*
y + (5*x*(a - Sqrt[a^2 + 6*a]))/(3*α*y) + (7*
x*(a - Sqrt[a^2 + 6*a]))/(3*α*t)))*
Sqrt[α]*δ)/(Sqrt[β +
1]*(η/E^(δ*(x + y)) + 4*(E^(δ*x))^2));

transforms = {(2*Sqrt*Sqrt[α]*δ)/Sqrt[β + 1] ->
Subscript[C, 1], (x*(a - Sqrt[a^2 + 6*a]))/(3*α) ->
Subscript[C, 2]};


The simpler the LHS of a rule, the easier it is to apply.

rule1 = Solve[(Equal @@ transforms[]) /. (Sqrt[α] -> sqrt),
sqrt][] /. sqrt -> Sqrt[α]

(* {Sqrt[α] -> (Sqrt[1 + β] Subscript[C, 1])/(2 Sqrt δ)} )*

rule2 = Solve[(Equal @@ transforms[]) /. (Sqrt[a^2 + 6*a] -> sqrt),
sqrt][] /. sqrt -> Sqrt[a^2 + 6*a]

(* {Sqrt[6 a + a^2] -> (a x - 3 α Subscript[C, 2])/x} *)

expr2 = expr /. rule1 /. rule2 // Simplify

(* (E^(I q y + (x + y) δ + ((7 I)/t + (5 I)/y) Subscript[C,
2]) Subscript[C, 1])/(4 E^((3 x + y) δ) + η) *)


Verifying,

expr == expr2 /. (Reverse /@ transforms) // Simplify

(* True *)
`