I am researching how to extract a certain value from an expression by using a pattern mechanism without actually changing the original expression.
The paragraph above could be very ambiguous, so here is an example. The aim here is to extract the number 4 before t and assign it to Symbol j.
Clear["Global`*"]; n = 3 Cos[4 t + 2] (* 3 Cos[2 + 4 t] *) n = 3 Cos[4 t + 2] /. _ Cos[r_ t + _] :> (k = r) (* 4 *) k (* 4 *) n (* 4 *) But unfortunately, it assigns 4 to n as well. Even if I use ; after k = r, it assigns Null to n as you can see below.
Clear["Global`*"] n = 3 Cos[4 t + 2] (* 3 Cos[2 + 4 t] *) n = 3 Cos[4 t + 2] /. _ Cos[r_ t + _] :> (k = r;) k (* 4 *) n How to extract 4 and assign it to k without having the original expression changed?