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

Re: s/// and \n question

Thread Previous | Thread Next
From:
Jim Gibson
Date:
July 21, 2011 14:28
Subject:
Re: s/// and \n question
Message ID:
CA4DE809.1360F%JimSGibson@gmail.com
On 7/21/11 Thu  Jul 21, 2011  2:11 PM, "Mike McClain" <mike.junk@cox.net>
scribbled:

> 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
> 00000000  39 38 37 33 32 31 0a                              |987321.|
> 00000007

Because the newline is not matched as part of /^.*?(\d+).*$/
Therefore, it is not replaced and is left in the string.

The .* does not gobble up the newline because . does not match newlines
unless the /s modifier is used. The $ is an anchor and does not match any
character.



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