I have a list expr of symbols.
Clear["Global`*"]; $Version (* 14.1.0 for Mac OS X ARM (64-bit) (July 16, 2024) *) expr = {a,d,c,b,f,c,a,X,X,a,b,b,X,b,f,c,b,d,a,f,X,d,d,c,X,X,f,f,c,f}; I want to move all X's to the right. This goal can be achieved by
wanted = Join[DeleteCases[expr, X], Cases[expr, X]] (* {a,d,c,b,f,c,a,a,b,b,b,f,c,b,d,a,f,d,d,c,f,f,c,f,X,X,X,X,X,X} *) But I want a rule-based solution using ReplaceRepeated[] because this is suitable for my ultimate goal. I tried
rule = {{p___, X, q_, r___} :> {p, q, X, r}}; expr2 = expr //. rule (* {a,d,c,b,f,c,a,X,X,a,b,b,X,b,f,c,b,d,a,f,X,d,d,c,X,X,f,f,c,f} *) My question is: Why ReplaceRepeated[] stopped there? Am I doing something wrong?
qmatchesX, the replacement results in the same expression. Would that cause it to stop? $\endgroup${a, X, c, d, X, b, X} //. {{p___, X, q_, r___} :> (Print[{"q" -> q, #}]; #) &@{p, q, X, r}}? It prints the replacement for each match. lericr has already explained the point in my comment. $\endgroup${{p___, X, q : Except[X], r___} :> {p, q, X, r}}or{{p___, X, r__} :> {p, r, X}}, it will work. $\endgroup$