On 21/07/2011 22:11, 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
00000000 39 38 37 33 32 31 0a |987321.|
00000007
Hey Mike
As others have noted, /./ without the /s modifier matches any character
but newline, so the regex matches only up to just before the "\n". But
your code would be better written as
use strict;
use warnings;
my $str = "asdfggfh 987321qwert99yyy\n";
my ($num) = $str =~ /(\d+)/;
print $num;
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/