Front page | perl.beginners |
Postings from July 2011
Re: s/// and \n question
Thread Previous
|
Thread Next
From:
Rob Dixon
Date:
July 21, 2011 17:54
Subject:
Re: s/// and \n question
Message ID:
4E28CA61.4090209@gmx.com
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
Thread Previous
|
Thread Next