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

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

Thread Previous | Thread Next
From:
Darren Duncan
Date:
March 27, 2021 03:46
Subject:
Re: Let's talk about trim() so more
Message ID:
73ae6d86-19bd-7436-0b2b-3edf009c5799@darrenduncan.net
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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About