4
$\begingroup$

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 ?

$\endgroup$
2
  • $\begingroup$ can you please enter the problem is Mathematica synatx? Is the problem meant to be: problem = a { b { c d } e } a a { b b c c } ? I do not understand what path node list of the tree information here. You mean TreeForm ? can you give a link that explains what it is? $\endgroup$
    – Nasser
    Commented Jan 17, 2015 at 9:02
  • $\begingroup$ now modified. sorry for ambiguity. $\endgroup$
    – marsiano
    Commented Jan 17, 2015 at 9:08

1 Answer 1

5
$\begingroup$

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"}} *)
$\endgroup$
2
  • $\begingroup$ Thanks Woods. I think you gave me a correct answer! $\endgroup$
    – marsiano
    Commented Jan 19, 2015 at 2:34
  • $\begingroup$ some explanation would have been helpful. $\endgroup$ Commented Dec 12, 2018 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.