Mathematica keeps rewriting expressions, so it is hard to figure what pattern to use.
I am trying to replace all occurrences of $\frac{y}{x}$ by $t$, but Mathematica re-writes $\frac{1}{\left(\frac{y}{x}\right)}$ to $\frac{x}{y}$ so pattern y/x fails sometimes depending on if it is in the numerator or denominator.
So switched to using at FullForm and checking for all combinations. But that also does not work, and I am sure I will miss some cases.
Here is an example
ClearAll[t, x, y]
expr = (y/x)^2;
FullForm[expr]
Gives
Times[Power[x,-2], Power[y,2]]
So why does the below not match?
expr/.Times[Power[x,-(any0_.)], Power[y,any0_.]] :> t^any0
I am literally writing the same exact full form! But I changed the power to be anything. So in this case any0_. should match the 2, right? But it does not:

It turned out removing the minus sign before any0 above made it work, but I had to give the other power a different pattern name
expr/.Times[Power[x, any0_.], Power[y, any1_.]] :> t^any0

But that is not what I want. It should be $t^2$, I want the same pattern/power on both, but one with a minus sign to match. It looks like a when there is a minus sign there is a problem.
The main problem is really this: How do I change all (y/x)s anywhere in the expression to t so (y/x)^3 will change to t^3 as example?
The problem is also that Mathematica rewrites the expression internally so it is hard to know what pattern to use. What I am looking at is not what it is internally.
I even used the excellent function by Carl Woll:
getPatterns[expr_, pat_] := Last@Reap[expr /. a : pat :> Sow[a], _, Sequence @@ #2 &];
getPatterns[(y/x)^2, (y/x)^any_.]
But that did not pick this due to the re-writing.
{}
I can't use HoldForm either on these things. Any idea how to do this which will work all the time?
Examples to test with
expr = (1 + 2 (y^2/x^2))/(2 (y/x))

This should be transformed to $\frac{1+ 2 t^2}{2 t}$
expr = y/x + Sqrt[1 + (y/x)^2]

It should be transformed to $t+\sqrt{1+t^2}$
Note: It is not required that the pattern transforms things like y^4/x^3 to y t^3. It can be assumed that y/x always shows with same power. But if the transformation can handle this general case, it will be even better, but not required.


x^3/y^5? Should it be left unchanged, replaced by1/(t^3 y^2)or by1/(t^5 x^2)? – jkuczm Jul 29 '17 at 10:48x^3/y^5should become(1/t^3 y^2)but for now, this is not required, as I make sure I enter the expression with terms as(y/x)^nonly. i.e. the expression will contain only terms of(y/x)of some powers. I am keeping it simple. But if the code will also support the general case you showed, I will not complain ofcourse :) – Nasser Jul 29 '17 at 10:55HoldFormin these examples? As I attempted to illustrate in my answer that substantially changes the problem. – Mr.Wizard Jul 29 '17 at 10:59HoldFormfor illustration of what the input look like on the screen. Nothing more. Else mathematica will re-write it and becomes hard to see they/xpattern. I do not useHoldFormat all in the code. Will removeHoldFormnow so not be confusing. – Nasser Jul 29 '17 at 11:01