Regular expression questions

FP

Diamond Member
Feb 24, 2005
4,568
0
0
Anyone know why these print different things?

Why does adding an end-of-line assertion on the second regexp cause it to be less greedy?

#!/usr/bin/perl -w

use strict;

my $str1 = ' foobar ';
$str1 =~ s/^\s*(.*?)\s*/$1/;
print $str1 . "#\n";

my $str2 = ' foobar ';
$str2 =~ s/^\s*(.*?)\s*$/$1/;
print $str2 . "#\n";