while ($str =~ s/\A(foo)//) { push @got, $1; } die "unexpected stuff in string: $str" if $str ne '';