$string =~ s/^\S+//; # assumed to remove *all* whitespaces from the beginning $string =~ s/\S+$//; # assumed to remove *all* whitespaces from the end $string =~ s/^\s+|\s+$//g; # /g needed here