I would like to be able to have a pattern that matches only expressions that are (alternately: are not) children of certain other elements.
For example, a pattern to
I am probably misunderstanding you, but, if I do understand correctly you want to match all expressions with head List
which have the property that, going upwards in the expression tree, we'll never meet a Graphics
. I m not sure how to do this in one pass, but if you are willing to match twice, you can do something like
lst = {randhead[5], {1, 2, {3, 5}}, Graphics[Line[{{1, 2}, {3, 4}}]]};
Cases[#, _List] &@Cases[#, Except@Graphics[___]] &@lst
(*
----> {{1, 2, {3, 5}}}
*)
which first selects elements so that the Head
isn't Graphics
(this is done by Cases[#, Except@Graphics[___]] &
, which returns {randhead[5], {1, 2, {3, 5}}}
), then selects those with Head
List
from the returned list. Note that I've added some more stuff to lst
.
But presumably you knew this and were after a single pattern to do the job?