my $str = '1234 6789 1234 6789 1234 6789 '; my $printstr = substr($str, 0, 20+1); $printstr =~ s/\W+\w*$//; print $printstr;