# Extract subsequence between two specified numbers

I have a list as follows:

{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}


I want to split it into many sublists where each sublist is extract between

35793, 21592


and

10


For example this list

{105, 110, 103, 63}


is the first sublist from the sample.

• Take a look at SequenceCases.
– Kuba
Mar 22, 2017 at 7:27

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}}

• For SequenceCases, it's V10.1 or newer. I know this because I'm stuck at V10.0 :( Mar 22, 2017 at 16:11
• @march, thank you. Corrected the version.
– kglr
Mar 22, 2017 at 17:41
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.

list =
{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};


Using SequenceSplit (new in 11.3)

Cases[{a__, 10, __} :> {a}] @ SequenceSplit[list, {35793, 21592}]


{{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}}