I would like to turn this:
mystr = ' foo1 (foo2 foo3 (foo4))'
['foo1','foo2 foo3 (foo4)']
As far as I can understand, this is consistent with what you want, and is pretty simple. It just uses some slicing to isolate the first word and the part between parentheses. It also has to use
strip a couple of times due to the extra spaces. It may seem a little verbose, but to be honest if the task can be accomplished with such simple string operations I feel like complicated parsing is unnecessary (although I may have gotten it wrong). Note that this is flexible in the amount of whitespace to split by.
mystr = ' foo1 (foo2 foo3 (foo4))' mystr = mystr.strip() i = mystr.index(' ') a = mystr[:i].strip() b = mystr[i:].strip()[1:-1] print([a, b])
['foo1', 'foo2 foo3 (foo4)']
Although I'm still not entirely clear if this is what you want. Let me know if it works or what needs changing.