develooper Front page | perl.perl5.porters | Postings from March 2021

Re: Let's talk about trim() so more

Thread Previous | Thread Next
Darren Duncan
March 27, 2021 03:46
Re: Let's talk about trim() so more
Message ID:
On 2021-03-26 6:15 p.m., B. Estrade wrote:
> Apologies for another post, but rather than as, "well what single regex would 
> replace this" it was easy enough for me to just figure it out:
> perl -e '$x=" foo "; $x =~ s/^\s*([^\s]*)\s*$/$1/; print $x'

Its more like replacing this:

   print $x =~ s/^\s*(.*?)\s*$/r;

Your version seems to have the error in that it wouldn't do anything for inputs 
that also have whitespace in the middle, such as if we want to trim " foo bar " 
and get "foo bar".

-- Darren Duncan

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About