perl -le '$_="[987]ABC[GHI]XYZ[dfg]";print "$_" for grep{$_}split/(?!\w+\])|\[|]/,$_'