my @split; foreach (m/(.+?)(?:|\^A|\cA)/g) { s/\s+//g; push @split, $_; } if (!@split) { # print error here last; } # do stuff with @split here