re = /\A     
  (?<bb>     
    \[       
    \g<bb>*  
    \]       
  )*         
\z/x         

10.times do |i|
  s = (%w{[ ]} * i).shuffle.join
  puts (s =~ re? : ) + s
end

[, , ].each do |s|
  t = s.gsub(/[^\[\]]/, )
  puts (t =~ re? : ) + s
end