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.
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.
Or this:
s/\A\s*|\s*\Z//g
$x =~ s/(^\s+)|(\s+$)//g;
Funny you should bring this up!
I recently read an article analyzing the performance of twelve (!) different trim implementations.
Although the article specifically uses the JavaScript regex implementation, it uses Perl syntax, so I think it's apropos to this discussion.
I usually do it like this:
Everything between the leading spaces and the trailing spaces is grouped and returned, so I can assign it to the same old variable.