use strict; use Data::Dumper; my %hash; $_="ABCD----[this] fgab [that] BFTE-- [other] AB CD EF---- [foo] FOO[this][that][other] BAR []"; while ( /\G\s*([\w ]+)[\s-]*\[([\w\[\]]*)\]/g ) { $hash{$1}=$2; $hash{$1} =~ s/\]\[//g; } print Dumper (\%hash); #$VAR1 = { # 'BAR ' => '', # 'fgab ' => 'that', # 'ABCD' => 'this', # 'BFTE' => 'other', # 'FOO' => 'thisthatother', # 'AB CD EF' => 'foo' # };