develooper Front page | perl.beginners | Postings from July 2011

Re: s/// and \n question

Thread Previous | Thread Next
From:
Rob Dixon
Date:
July 22, 2011 08:10
Subject:
Re: s/// and \n question
Message ID:
4E2992DE.7010602@gmx.com
On 22/07/2011 16:05, Mike McClain wrote:
> On Thu, Jul 21, 2011 at 02:11:33PM -0700, Mike McClain wrote:
>>
>> Given the following snippet of code could someone explain to me
>> why the linefeed gets included in $num?
>>
>> mike@/deb40a:~/perl>  perl -e'
>> $str = "asdfggfh 987321qwertyyy\n";
>> ($num = $str) =~ s/^.*?(\d+).*$/$1/;
>> print $num;
>> ' | hd
>
> My thanks to Shawn, Jim, Rob&  Dr. Rudd for their answers.
> Now I not only know why I was getting the '\n' but 2 ways to avoid it.
>
> ($num = $str) =~ s/^.*?(\d+).*$/$1/s;
> or
> ($num) = $str =~ /^.*?(\d+).*$/;

Again, there is no need for all that noise in the regex.

   my ($num) = $str =~ /(\d+)/;

is fine.

Rob

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