Is there a Perl-compatible regular expression to trim whitespace from both sides of a string?
问题 Is there a way to do this in one line? $x =~ s/^\s+//; $x =~ s/\s+$//; In other words, remove all leading and trailing whitespace from a string. 回答1: $x =~ s/^\s+|\s+$//g; or s/^\s+//, s/\s+$// for $x; 回答2: My first question is ... why? I don't see any of the single-regexp solutions to be any more readable than the regexp you started with. And they sure aren't anywhere near as fast. #!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all); my $a = 'a' x 1_000; my @x = ( " $a ", "$a ",