title: How can I extract some lines from a string while adding parentheses to the resulting string where I need it?
body: <p>I have a python string which contains the output of a Metric-FF planner that looks like this:</p>
  <pre class="lang-none prettyprint-override"><code>ff: parsing domain file
  domain 'BRILLOPROVA' defined
   ... done.
  ff: parsing problem file
  problem 'TASK' defined
   ... done.
  
  
  translating negated cond for predicate DIRTY-BLENDER
  translating negated cond for predicate EMPTY-BLENDER
  no metric specified.
  
  ff: search configuration is Enforced Hill-Climbing, if that fails then best-first search.
  Metric is plan length.
  NO COST MINIMIZATION (and no cost-minimizing relaxed plans).
  
  Cueing down from goal distance:    6 into depth [1]
                                     5            [1][2][3]
                                     4            [1]
                                     3            [1]
                                     2            [1]
                                     1            [1]
                                     0            
  
  ff: found legal plan as follows
  step    0: PICK-UP-GLASS H
          1: PICK-FLAVOR H PAPAYA VASCHETTA
          2: ADD-JUICE MELA H PAPAYA VASCHETTA
          3: BLEND MELA PAPAYA H
          4: TRASH-CUP VASCHETTA H
          5: FILL-GLASS H
          6: WASH-BLENDER
          7: SERVE LEFT
  
  time spent:    0.00 seconds instantiating 10 easy, 0 hard action templates
                 0.00 seconds reachability analysis, yielding 14 facts and 10 actions
                 0.00 seconds creating final representation with 14 relevant facts, 0 relevant fluents
                 0.00 seconds computing LNF
                 0.00 seconds building connectivity graph
                 0.00 seconds searching, evaluating 12 states, to a max depth of 3
                 0.00 seconds total time
  </code></pre>
  <p>and I need to parse it so that the resulting string looks like this:</p>
  <pre><code>PICK-UP-GLASS PICK-FLAVOR(PAPAYA) ADD-JUICE BLEND TRASH-CUP FILL-GLASS WASH-BLENDER SERVE(LEFT)
  </code></pre>
  <p>So I need only the sequence of the actions, keeping only some parameters for the actions &quot;PICK-FLAVOR&quot; (precisely it will be the name of a fruit), and &quot;SERVE&quot; (which can have only &quot;LEFT&quot;, &quot;RIGHT&quot; and &quot;CENTER&quot; as a parameter).
  Keeping in mind that words like the parameters &quot;VASCHETTA&quot; and &quot;H&quot; and the names of the actions (&quot;PICK-UP-GLASS&quot;, &quot;PICK-FLAVOR&quot;, &quot;ADD-JUICE&quot;, &quot;BLEND&quot;, &quot;TRASH-CUP&quot;, &quot;FILL-GLASS&quot;, &quot;WASH-BLENDER&quot;, &quot;SERVE&quot;) are already known to me, so I can get rid of them easily with regex, but words like &quot;MELA&quot;, &quot;PAPAYA&quot; and &quot;LEFT&quot; can vary witch each output, so I don't know them in advance and can't seem to manipulate the string to get the result I want.</p>
  <p>I've tried the following code to trim the string at the best of my abilities:</p>
  <pre><code>import re
  string = &quot;&quot;&quot;
  ff: parsing domain file
  domain 'BRILLOPROVA' defined
   ... done.
  ff: parsing problem file
  problem 'TASK' defined
   ... done.
  
  
  translating negated cond for predicate DIRTY-BLENDER
  translating negated cond for predicate EMPTY-BLENDER
  no metric specified.
  
  ff: search configuration is Enforced Hill-Climbing, if that fails then best-first search.
  Metric is plan length.
  NO COST MINIMIZATION (and no cost-minimizing relaxed plans).
  
  Cueing down from goal distance:    6 into depth [1]
                                     5            [1][2][3]
                                     4            [1]
                                     3            [1]
                                     2            [1]
                                     1            [1]
                                     0            
  
  ff: found legal plan as follows
  step    0: PICK-UP-GLASS H
          1: PICK-FLAVOR H PAPAYA VASCHETTA
          2: ADD-JUICE MELA H PAPAYA VASCHETTA
          3: BLEND MELA PAPAYA H
          4: TRASH-CUP VASCHETTA H
          5: FILL-GLASS H
          6: WASH-BLENDER
          7: SERVE LEFT
  
  time spent:    0.00 seconds instantiating 10 easy, 0 hard action templates
                 0.00 seconds reachability analysis, yielding 14 facts and 10 actions
                 0.00 seconds creating final representation with 14 relevant facts, 0 relevant fluents
                 0.00 seconds computing LNF
                 0.00 seconds building connectivity graph
                 0.00 seconds searching, evaluating 12 states, to a max depth of 3
                 0.00 seconds total time
  &quot;&quot;&quot;
  list = string.split(&quot;&quot;&quot;ff: found legal plan as follows
  step&quot;&quot;&quot;)
  list = list[1].split(&quot;time spent:&quot;)
  steps = list[0]
  steps = steps.translate({ord(i): None for i in '0123456789:'})
  steps = re.sub(&quot;\s+H\s+&quot;, &quot; &quot;, steps)
  steps = re.sub(&quot;\s+VASCHETTA\s+&quot;, &quot; &quot;, steps)
  steps = re.sub(&quot;ADD-JUICE .* BLEND&quot;, &quot;ADD-JUICE BLEND&quot;, steps)
  steps = re.sub(&quot;BLEND [A-Z]* [A-Z]* &quot; ,&quot;BLEND &quot; , steps)
  list = steps.split(&quot;PICK-FLAVOR &quot;)
  steps = list[0] + &quot;PICK-FLAVOR(&quot; + list[1]
  print(steps)
  </code></pre>
  <p>with this result:</p>
  <pre><code> PICK-UP-GLASS PICK-FLAVOR(PAPAYA ADD-JUICE BLEND TRASH-CUP FILL-GLASS WASH-BLENDER
       SERVE LEFT
  </code></pre>
  <p>So I'm almost there, but I don't know how to add the rest of the parentheses.</p>