# Replace rule using a wildcard

I have a complicated expression which I want to simplify following a rule. For example, if I want to make the following replacement:

A[t] - B*A[t - d1] = Y[t]


for any values of t and d1, for example,

A[t - 4] - B*A[t - 4 - d1] = Y[t-4]


Using the wildcard underscore notation, I've been able to get a simple version to work,

A[t-4] - B*A[-d1 + t-6] /. A[t_] - B*A[d_] ->Y[t]


which correctly returns Y[t-4]. However, if everything is premultiplied, then I cannot get the right result:

(x1*A[t - 4] - x1*B*A[-d1 + t - 4]) /. {A[t_] - B*A[d_] -> Y[t]}


which should give x1*Y[t-4]. I understand that if I look at the FullForm that Mathematica recognizes it as something different, but I need a way around that. I don't think I can easily collect terms as my expressions are something like:

(x1*A[t - 4] - x1*B*A[-d1 + t - 4]) + (x1*x2*A[t] - x1*x2*B*A[-d1 + t])


and I need a rule which changes that to x1*Y[t-4] + x1*x2*Y[t]. I was thinking about using another wildcard before the terms, but this doesn't appear to be working:

(x1*A[t - 4] - x1*B*A[-d1 + t - 4]) /. {x_*A[t_] - x*B*A[d_] -> x*Y[t]}


Any help or a simple way to make these replacements would be much appreciated!

rule = {a_. A[t_] - a_. B*A[d_] :>  a Y[t]};


Use it with ReplaceAll or ReplaceRepeated:

(A[t - 4] - B*A[-d1 + t - 4]) /. rule


Y[-4 + t]

(x1*A[t - 4] - x1*B*A[-d1 + t - 4]) /. rule


x1 Y[-4 + t]

(x1*A[t - 4] - x1*B*A[-d1 + t - 4]) + (x1*x2*A[t] - x1*x2*B*A[-d1 + t]) //. rule


x1 Y[-4 + t] + x1 x2 Y[t]

• This is great, thank you! very clean solution – mike_s Oct 30 '18 at 18:23
• @mike_s, my pleasure. Thank you for the accept and welcome to mma.se. – kglr Oct 30 '18 at 18:23