Say I have this list:
list = {0, 1, 2, 0, 1, 3, 0, 1, 4}
and say I want to remove consecutive sequences of 0 and 1. I'd normally go for this:
list //. {before___, 0, 1, after___} :> {before, after}
{2,3,4}
But a more concise way would be the following:
list /. PatternSequence[0, 1] -> Sequence[]
Alas, this doesn't do anything. The examples in the PatternSequence documentation are all of the form
list //. {before___, PatternSequence[0, 1], after___} :> {before, after}
which kind of defeat the purpose. (It's pretty useful if you want to name the PatternSequence on the LHS of the rule, but that's not my aim).
So the question is, is there a way to use constructs like PatternSequence[0, 1] -> Sequence[] in ReplaceAll?
PatternSequenceis that now one can name sequences of patterns for restructuring. This is an important feature, but is not the same as what you are looking for. – Leonid Shifrin Jul 14 '14 at 18:41list //. {before___, 0, 1, after___} :> afteronly match the first instance? Whereas,{a, b, a} //. a :> zmatches both instances ofa– alancalvitti Apr 07 '18 at 04:35