Если все основные текстовые группы разделены запятой с пробелом, вы можете использовать re.split
:
import re
b='hi, this(me,(you)) , hello(a,b)'
result = re.split(',\s', b)
Вывод:
['hi', 'this(me,(you)) ', 'hello(a,b)']
Однако вы можете также используйте рекурсию для разбора строки:
def parse(d):
if (v:=next(d, None)) is not None and v != ')':
yield v if v != '(' else f'({"".join(parse(d))})'
yield from parse(d)
r, c, l = list(filter(lambda x:x != ',', parse(iter(re.findall('\w+|\(|\)|,', b))))), '', []
for i in r:
if not i.endswith(')'):
l.append(c)
c = i
else:
l.append(c+i)
c = ''
l.append(c)
final_result = list(filter(None, l))
Вывод:
['hi', 'this(me,(you))', 'hello(a,b)']