fork download
  1. import re
  2.  
  3. x = "test1 21"
  4. y = " test2 22"
  5. z = " test3 [ 23 ]"
  6.  
  7. for a in [x, y, z]:
  8. print(re.sub(r"(?<![^[\s])\s+|\s+(?=])", "", a.rstrip('\n').lstrip(' ').rstrip(';')).split(' '))
Success #stdin #stdout 0.02s 9280KB
stdin
Standard input is empty
stdout
['test1', '21']
['test2', '22']
['test3', '[23]']