I am trying to match expressions that are the sum of two or more objects with a specific head. However I can not get this work properly.
For example
obj[a]+obj[b] /. Plus[A:Repeated[_obj,{2,Infinity}] :> Hold[A]
returns
obj[a]+obj[b]
instead of
Hold[obj[a]+obj[b]]
as desired.
Somewhat confusingly allowing single repeats
obj[a]+obj[b] /. Plus[A:Repeated[_obj,{1,Infinity}] :> Hold[A]
does return
Hold[obj[a]]+Hold[obj[b]]
as expected. Also using a different overall head instead of "Plus" for example "List" also works
List[obj[a],obj[b]] /. List[A:Repeated[_obj,{2,Infinity}] :> Hold[A]
returns
Hold[obj[a],obj[b]]
Does anybody have an idea what is going on here (and how to achieve what I was trying in the first place)?
Pluson the left-hand-side of the rule evaluates, which changes your pattern. Tryobj[a] + obj[b] /. Verbatim[Plus][A : Repeated[_obj, {2, Infinity}]] :> Hold[A]instead. This question is a duplicate and I shall mark it as such in a minute. – Mr.Wizard Apr 14 '16 at 11:22