# How to match a pattern with exact occurence?

Maybe the way I expressed is not very accurate, but a very simple example will help to understand the question:

For

Subscript[a,i]+Subscript[a,j]**Subscript[a,k]


I would like to replace a pattern that a_i shows exactly one time with some other symbol.

An intuitive way fails to work using Except:

Subscript[a, i] + Subscript[a, j] ** Subscript[a, k] /.
Except[Subscript[a, x_] ** Subscript[a, y_], Subscript[a, z_]] -> zz


The output is zz + zz ** zz. However, I would like it to be zz+Subscript[a, j] ** Subscript[a, k].

The reason I need this is that I want to drop the linear term but keep the quadratic term of a_i by replacing only the linear term of a_i to zero.

• Look at Subscript[a, i] + Subscript[a, j] ** Subscript[a, k] // FullForm. That should explain why the pattern does not match. Apr 12, 2020 at 2:28
• @Rohit Namjoshi, Thanks! I understand why it fails now. But is there a workaround that can realize my idea? Apr 12, 2020 at 3:12
• Subscript[a, i] + Subscript[a, j] ** Subscript[a, k] /. Plus[a___, b_Subscript, c___] :> a + zz + c Apr 12, 2020 at 4:23

Subscript[a, i] + Subscript[a, j] ** Subscript[a, k] /.

  zz + Subscript[a, j] ** Subscript[a, k]

because ReplaceAll >> Details: