fork download
  1. import re
  2.  
  3. pattern = r"([^\s()]+\([^()]*\))|(\([^()]*\)[^\s()]+)|[()]"
  4. a = 'abc (xyz pqr) qwe ew (kjlk asd) ue(aad) kljl'
  5.  
  6. y = [x.strip() for x in re.split(pattern, a) if x and x.strip()]
  7. print(y)
Success #stdin #stdout 0.03s 9712KB
stdin
Standard input is empty
stdout
['abc', 'xyz pqr', 'qwe ew', 'kjlk asd', 'ue(aad)', 'kljl']