On 11-07-21 08:54 PM, Rob Dixon wrote:
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
I think part of the confusion is also in what does $ match? According
to perlre, under "Regular Expressions",
$ Match the end of the line (or before newline at the end)
That means it can match the end of the string or a newline at the end of
the string.
Try this and see what it prints:
#!/usr/bin/env perl
use strict;
use warnings;
while( <> ){
print if /(\w+)$/
}
__END__
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/