"Rob Dixon"  wrote in message news:4d48bdc7.6050...@gmx.com...

On 01/02/2011 02:30, Chris Charley wrote:

#!/usr/bin/perl
use strict;
use warnings;

$_ = "[][12/21/10 18:39:22] [oasetup] [oasetup] [INFO] Installing the
HPOvXpl package...";

for my $re (qw{ ^\[(.+?)\] ^\[(.*?)\] }) {
my ($dt) = /$re/ or die "Horrible death\n";;
print $re, ' ', "'$dt'", "\n";
}

__END__
C:\Old_Data\perlp>perl t5.pl
^\[(.+?)\] '][12/21/10 18:39:22'
^\[(.*?)\] ''

C:\Old_Data\perlp>

My question in the code above is the '.+?' behavior. I had guessed
that it would attempt to match the empty brackets and fail, (because
it requires 1 or more characters). Instead, it captures the right
bracket and the left opening bracket and contents (date) of the
second bracket pair.

Does the regex first consume a character before checking the next
character to see if it is the first one *after* the expression the
'+?' is applied to. It seems to be the way this regex behaved.

Or does the regex see its going to fail with the empty first bracket
pair, and so tries to advance to somehow find a match? This seems
pretty vague.

I thought a regex first looks for the next character beyond the
expression the '+?' is applied to, before it consumes any characters?
That is the way the second regex, '.*?', behaved.

Hope it was clear enough. :-)

Hey Chris

/.+?/ requires at least one character, so /^\[.+?\]/ will need at least
three characters for a match. The engine will try to match /^\[.\]/ and
fail, then /^\[..\]/, then /^\[...\]/ and so on. Perl doesn't care that
the object string's second character is a ']' - all it has to do is
match /./ and that will do fine. This process continues until the
pattern is long enough to reach the first closing square bracket it
sees, and so comsumes the date/time field to get there.

/.*?/ will match zero characters, so the initial [] fits the bill
immediately. The ? doesn't change the lower bound of the character count
- it simply says to match as few as possible. /.{2,4}/ will match four
characters, while /.{2,4}?/ will match two.

HTH,

Rob

Hi Rob,

Thanks for the good answers from you and Uri. Yes, fulfilling the min of a quantifier before applying the lazy behavior of the ? is what had me stumped. The ? doesn't change the lower bound and I was hazy in my mind about that.

Chris

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to