I have a list of strings called mylist:
mylist = {"[a]", "a", "a", "[b]", "b", "b", "[ c ]", "c", "c"}; I would like to split mylist by "section headers." Strings that begin with the character [ are section headers in my application. Thus, I would like to split mylist in such a way as to obtain this output:
{{"[a]", "a", "a"}, {"[b]", "b", "b"}, {"[ c ]", "c", "c"}} (The as, bs, and cs represent any characters; the string inside the section header does not necessarily match the strings that follow in that section. Also, the number of strings in each section can vary.
I have tried:
SplitBy[mylist, StringMatchQ[#, "[" ~~ ___] &] But this is not correct; I obtain:
{{"[a]"}, {"a", "a"}, {"[b]"}, {"b", "b"}, {"[ c ]"}, {"c", "c"}} Likewise, using Split (since it applies the test function only to adjacent elements) does not work. The command:
Split[mylist, StringMatchQ[#, "[" ~~ ___] &] yields:
{{"[a]", "a"}, {"a"}, {"[b]", "b"}, {"b"}, {"[ c ]", "c"}, {"c"}} Do you have any advice? Thanks.