# Replacement using pattern matching?

Starting with the expression

eq1 = 5 Exp[1 + 2 x + 3 x^2 + 4 x^3];


I want to implement the following transformation:

eq1 /.  Exp[a_ x^2 + b_ x^3] ->   f[a/b]


However, I am not getting the desired output $$5 f[3/4]\exp(1+2 x)$$.

Further, I also need to perform the following transformation.

eq2 = c Exp[a SuperStar[\[Alpha]] + b \[Alpha]];
eq2 /. d1_ Exp[d2 SuperStar[\[Alpha]_]] -> f[d2]


I am providing the above expressions again in latex font for clarification. $$\text{eq2}=c \exp \left(a \alpha ^*+\alpha b\right);$$

$$\text{eq2}\text{/.}\, \text{d1\_} \exp \left(\alpha \_{}^* \text{d2}\right)\to f(\text{d2})$$

Any help will be appreciated.

eq1 /. Exp[a_. x^2 + b_. x^3 + c_.] -> Exp[c] f[a/b]


5 E^(1 + 2 x) f[3/4]

eq2 /. d1_. Exp[d2_. SuperStar[α_] + d3_.] -> Exp[d3] f[d2]


E^(b α) f[a]

• If you use defaults in the replacement pattern (dots after the patterns), they become more versatile: Exp[a_. x^2 + b_. x^3 + c_.] -> Exp[c] f[a/b]. This will also work with eq1a = 5 Exp[x^2 + 4 x^3], for example. Also, d1_. Exp[d2_. SuperStar[α_] + d3_.] -> Exp[d3] f[d2] for the second one. – Roman May 9 '19 at 12:29
• @Roman, great point. Updated with default patterns. – kglr May 9 '19 at 12:49
• @Shadowray, thank you; I added the needed _.s. – kglr May 9 '19 at 13:52