#!/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. :-)

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to