# How can I set up patterns when using SequenceSplit?

Given a list

lst={0,0,0,0,0,"A1",0,0,0,"B2",0,0,"C3","D4","E5",0,0,0,"F6"}


how can I split it as

{{0,0,0,0,0},{"A1",0,0,0},{"B2",0,0},{"C3"},{"D4"},{"E5",0,0,0},{"F6"}}


I want to use SequenceSplit, but I don't know how to set up the right pattern.

SequenceSplit[lst, {_String} -> {}]


Split[lst, Not @ StringQ @ #2 &]


{{0, 0, 0, 0, 0}, {"A1", 0, 0, 0}, {"B2", 0, 0}, {"C3"}, {"D4"}, {"E5", 0, 0, 0}, {"F6"}}

SequenceSplit[lst, {s_String, a : Except[_String] ...} :> {s, a}]


{{0, 0, 0, 0, 0}, {"A1", 0, 0, 0}, {"B2", 0, 0}, {"C3"}, {"D4"}, {"E5", 0, 0, 0}, {"F6"}}

• Very nice,Thanks! what do #1 and #2 mean in Split function? Commented Jan 13, 2019 at 3:49
• @Jerry see Working with Pure Functions and Slot Commented Jan 13, 2019 at 3:56
• kglr, do you have something against !? :^) Commented Jan 13, 2019 at 3:57
• @Mr.Wizard, thank you.
– kglr
Commented Jan 13, 2019 at 4:01
• @Jerry, in a test function used as the second argument of Split, #1 refers to the first element of a consecutive pair and #2 to the second, See also animation - Split which illustrates how it works for the default test function SameQ.
– kglr
Commented Jan 13, 2019 at 4:04
list =
{0, 0, 0, 0, 0, "A1", 0, 0, 0, "B2", 0, 0, "C3", "D4", "E5", 0, 0, 0, "F6"};

SequenceSplit[list, x : {_, 0 ...} :> x]


{{0, 0, 0, 0, 0}, {"A1", 0, 0, 0}, {"B2", 0, 0}, {"C3"}, {"D4"}, {"E5", 0, 0, 0}, {"F6"}}

list = {0, 0, 0, 0, 0, "A1", 0, 0, 0, "B2", 0, 0, "C3", "D4", "E5", 0, 0, 0, "F6"};


An alternative is to use SequenceCases:

patts = {s_String, a : Except[_String] ...} | {a : Except[_String] ..} :> {s, a};

SequenceCases[lst, patts]


{{0, 0, 0, 0, 0}, {"A1", 0, 0, 0}, {"B2", 0, 0}, {"C3"}, {"D4"}, {"E5", 0, 0, 0}, {"F6"}}