$s = ' input reset ; ';; $s =~ s[(?!^)(\s+)(?!$)][]g;; ## replace all spaces -- that are neither preceded by the start of string; nor followed by the end-of-string -- with nothing. print "'$s'";; ' inputreset; '