my %matches = $string =~ m!(]*>)(.+?]*>)!ig; foreach my $key (keys %matches) { print "$key\n"; print "$matches{$key}\n\n"; }