Extract subsequence between two specified numbers
lst = {65311, 10, 35793, 21592, 105, 110, 103, 63, 10, 19978, 35785,
20154, 65306, 78, 111, 46, 10, 35793, 21592, 65306, 20182, 35828,
27809, 26377, 12290, 10, 26816, 23519, 23448, 65306, 37027, 20320,
21644, 74, 117, 108, 105, 101, 116, 21435, 21335, 20140, 26159,
24178, 20160, 20040, 30340, 65292, 37027, 19968, 27425, 65311, 10,
35793, 21592, 65306, 84, 104, 101, 110, 32, 119, 104, 121, 32, 100,
105, 100, 32, 121, 111, 117, 32, 116, 119, 111, 32, 103, 111, 32,
116, 111, 32, 78, 97, 110, 106, 105, 110, 103, 63, 10, 19978,
35785, 20154, 65306, 73, 32, 119, 97, 115, 32, 100, 111, 105, 110,
103, 32, 109, 121, 32, 115, 99, 104, 111, 111, 108, 32, 112, 114,
111, 98, 108, 101, 109, 44, 32, 121, 111, 117, 32, 107, 110, 111,
119, 46, 32, 73, 32, 104, 97, 100, 32, 97, 32, 108, 111, 116, 32,
111, 102, 32, 115, 116, 117, 102, 102, 44, 32, 97, 32, 108, 111,
116, 32, 111, 102, 32, 115, 99, 104, 111, 111, 108, 32, 115, 116,
117, 102, 102, 44, 32, 121, 111, 117, 32, 107, 110, 111, 119, 46,
10, 35793, 21592, 65306, 83, 99, 104, 111, 111, 108, 32, 115, 116,
117, 102, 102, 63};
If you have version 10.1 or a newer version you can use SequenceCases
. For earlier versions you can use ReplaceList
.
SequenceCases[lst, {35793, 21592, x : Except[10] .., 10} :> {x}]
ReplaceList[lst, {___, 35793, 21592, x : Except[10] .., 10, ___} :> {x}]
both give
{{105, 110, 103, 63}, {65306, 20182, 35828, 27809, 26377, 12290}, {65306, 84, 104, 101, 110, 32, 119, 104, 121, 32, 100, 105, 100, 32, 121, 111, 117, 32, 116, 119, 111, 32, 103, 111, 32, 116, 111, 32, 78, 97, 110, 106, 105, 110, 103, 63}}
SequenceCases[list, {35793, 21592, sublist : Except[10]..., 10} :> {sublist}]
This ensures you capture all cases including patterns at the boundaries and the middle, including null patterns with nothing inbetween.