Extracting sequences from a list?
Try this one
ReplaceList[expr, {__, PatternSequence["Open",v__ /; Count[{v}, _String] == 0,
_String], __} -> {v}]
Here's a plain pattern approach, I'm not quite sure how robust it is:
ReplaceList[expr, {___, "Open", x : Except["Close"] ..., "Close", ___} :> {x}]
Also take a look at Longest
and Shortest
, which may come in handy.
In versions 10.1+, you can use SequenceCases
:
lst={1, 2, 3, "Open", 3, 2, "Close", 9, 3, 4, "Open", 1, 0, "Close", 3, 5};
SequenceCases[lst, {"Open", x:Except["Close"].., "Close"} :> {x}]
{{3, 2}, {1, 0}}