$text =~ s{ # Substitute \A # from the beginning of the string \s+ # one or more white space characters }{}xms; # with nothing $text =~ s{ # Substitute \s+ # one or more white space characters \z # from the end of the string }{}xms; # with nothing $text =~ s{ # Substitute \s+ # one or more white space characters }{ }gxms; # with one space, anywhere in the string # As many times as it occurs