$command =~ m/(!\w+\s?.+?)(?=!\w)/simg; #### my @commands = $command =~ m/(!\w+\s?.+?)(?=!\w)/simg; #### my @commands = $command =~ m/(!\w+\s?.+?)(?=!\w|\z)/simg; #### foreach my $command (@commands) { ... }