I am trying to parse a string, but I have no idea how to solve the following problem
problem = "a { b { c d } e } aa { bb cc }";
answer = {{"a", "b", "c"}, {"a", "b", "d"}, {"a", "e"}, {"aa", "bb"}, {"aa", "cc"}};
So the answer is a path in each sub-list of the list
Any suggestions ?
Answer
I may have misunderstood the requirement but this seems to work for the example at least:
problem = "a { b { c d } e } aa { bb cc }";
Map[Flatten, StringSplit[problem] //.
{head___, x_, "{", Shortest[y__], "}", tail___} :>
{head, Sequence @@ Thread[{x, {y}}], tail}]
(* {{"a", "b", "c"}, {"a", "b", "d"}, {"a", "e"}, {"aa", "bb"}, {"aa", "cc"}} *)
Comments
Post a Comment