use strict; use warnings; my $str="A: Gold, Black, Purple, Blue, Red B: Black, Neon Pink, Neon Yellow, Neon Green, Neon Purple, Red, White, Neon Orange, Navy"; my %arrays; my( @substrings ) = $str =~ m/(\w:\s*.+?)(?=\w:|$)/g; foreach my $substring ( @substrings ) { my( $key, $colors ) = split /:\s*/, $substring; my( @colors ) = split /,\s*/, $colors; $arrays{ $key } = [ @colors ]; } foreach my $key ( sort keys %arrays ) { print "Array $key: @{$arrays{$key}}\n"; }