Python用空格和括号分割未知字符串

Tre*_*ton 1 python string split

如果我有一个字符串可能是:

'Hello (Test1 test2) (Hello1 hello2) other_stuff'
Run Code Online (Sandbox Code Playgroud)

我想把它分成这样的东西:

split1='hello'
split2='Test1, test2'
split3='Hello1, hello2'
split4='other_stuff'
Run Code Online (Sandbox Code Playgroud)

然后我将把它放入一个变量中:

full_split=[split1, split2, split3, split4]
Run Code Online (Sandbox Code Playgroud)

而且,未知的字符串是,如果他们继续在末尾添加单词,它将继续添加分割变量(split5, split6

我正在研究正则表达式,但我不喜欢导入 python 不附带的模块。如果必须的话我会的。

Ash*_*ary 5

使用regex,str.splitstr.join:

>>> import re
>>> strs = "Hello (Test1 test2) (Hello1 hello2) other_stuff"
>>> [", ".join(x.split()) for x in re.split(r'[()]',strs) if x.strip()]
['Hello', 'Test1, test2', 'Hello1, hello2', 'other_stuff']
Run Code Online (Sandbox Code Playgroud)