import re

x = "test1     21"
y = "     test2  22"
z = "    test3    [ 23 ]"

for a in [x, y, z]:
    print(re.sub(r"(?<![^[\s])\s+|\s+(?=])", "", a.rstrip('\n').lstrip(' ').rstrip(';')).split(' '))