$str='ABCD----[this] fgab [that] BFTE-- [other] AB CD EF---- [foo]'; $hash{$1}=$2 while ($str=~m#\s*([\w ]+)[\s-]+\[([^\]]+)\]#g); print "$_=$hash{$_}\n" for keys %hash; #### fgab=that ABCD=this BFTE=other AB CD EF=foo