>>>>> "CC" == Chris Charley <char...@pulsenet.com> writes:

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

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

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

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

  CC> C:\Old_Data\perlp>

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

the regex is working as you coded it. 

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

if a regex fails, it bumps the char pointer to the next data char and
starts over. but you have an anchor there so it must match at the
beginning. so it matches the leading [ as you ask. then it grabs until
it sees text followed by a ]. you got what you asked for.

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

nope. it is just what you said. the anchor forces it to grab the ] as it
satisfies the grab.

and without the anchor it still grabs the same thing since it does match
from the leading [ to a closing ] with text inside. the better way to
grab inside delimiters is to use a negated char class of the closing
delimiter. this grabs from a [ all the chars but a ] to the next ]. 

this works:

        qr{\[([^]]+)\]}

i used qr's instead of qw's but it comes out the same.

uri


-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
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