Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   qr makes pattern match fail (http://www.velocityreviews.com/forums/t899536-qr-makes-pattern-match-fail.html)

it_says_BALLS_on_your forehead 08-17-2006 06:24 PM

qr makes pattern match fail
 
i think i just need a fresh set of eyes. why would pre-compiling the
pattern make this fail?

#!/usr/bin/perl

use strict; use warnings;

my $entry = qr/login\/response/; # prints "did not match.".
# my $entry = 'login/response'; # prints "matched."

print $entry, "\n";

my $record = q{08/16/2006 00:00:01 GET
/ftgw/Fas/Fidelity/RtlCust/Login/Response blah blah};

if ( $record =~ m/$entry/i ) {
print "matched.\n";
}
else {
print "did not match.\n";
}


Paul Lalli 08-17-2006 06:31 PM

Re: qr makes pattern match fail
 
it_says_BALLS_on_your forehead wrote:
> i think i just need a fresh set of eyes. why would pre-compiling the
> pattern make this fail?
>
> #!/usr/bin/perl
>
> use strict; use warnings;
>
> my $entry = qr/login\/response/; # prints "did not match.".
> # my $entry = 'login/response'; # prints "matched."
>
> print $entry, "\n";
>
> my $record = q{08/16/2006 00:00:01 GET
> /ftgw/Fas/Fidelity/RtlCust/Login/Response blah blah};
>
> if ( $record =~ m/$entry/i ) {
> print "matched.\n";
> }
> else {
> print "did not match.\n";
> }


Relatively certain it's because your pre-compiled regular expression is
very specifically not case insensitive. The /i flag on the "outer"
regexp doesn't affect that. If you put the /i flag on the pre-compiled
regexp, it matches.

Paul Lalli


Ben Morrow 08-17-2006 06:46 PM

Re: qr makes pattern match fail
 

Quoth "it_says_BALLS_on_your forehead" <simon.chao@fmr.com>:
> i think i just need a fresh set of eyes. why would pre-compiling the
> pattern make this fail?
>
> #!/usr/bin/perl
>
> use strict; use warnings;
>
> my $entry = qr/login\/response/; # prints "did not match.".


If you use different delimiters you don't need the \.

> # my $entry = 'login/response'; # prints "matched."
>
> print $entry, "\n";


If you set $\ Perl will print the "\n" for you.

>
> my $record = q{08/16/2006 00:00:01 GET
> /ftgw/Fas/Fidelity/RtlCust/Login/Response blah blah};
>
> if ( $record =~ m/$entry/i ) {
> print "matched.\n";
> }
> else {
> print "did not match.\n";
> }


If you print the compiled regex you'll see why. qr// encapsulates the
state of the /ismx flags in the regex (it's part of the compiling
process), so you need

my $entry = qr{login/response}i;

Then you can match with just $record =~ $entry.

Ben

--
I have two words that are going to make all your troubles go away.
"Miniature". "Golf".
[benmorrow@tiscali.co.uk]

it_says_BALLS_on_your forehead 08-17-2006 07:09 PM

Re: qr makes pattern match fail
 

Paul Lalli wrote:
> it_says_BALLS_on_your forehead wrote:
> > i think i just need a fresh set of eyes. why would pre-compiling the
> > pattern make this fail?
> >
> > #!/usr/bin/perl
> >
> > use strict; use warnings;
> >
> > my $entry = qr/login\/response/; # prints "did not match.".
> > # my $entry = 'login/response'; # prints "matched."
> >
> > print $entry, "\n";
> >
> > my $record = q{08/16/2006 00:00:01 GET
> > /ftgw/Fas/Fidelity/RtlCust/Login/Response blah blah};
> >
> > if ( $record =~ m/$entry/i ) {
> > print "matched.\n";
> > }
> > else {
> > print "did not match.\n";
> > }

>
> Relatively certain it's because your pre-compiled regular expression is
> very specifically not case insensitive. The /i flag on the "outer"
> regexp doesn't affect that. If you put the /i flag on the pre-compiled
> regexp, it matches.



ahh, duh! thx Paul.


it_says_BALLS_on_your forehead 08-17-2006 07:50 PM

Re: qr makes pattern match fail
 

A. Sinan Unur wrote:
> "it_says_BALLS_on_your forehead" <simon.chao@fmr.com> wrote in
> news:1155839093.785576.270340@m79g2000cwm.googlegr oups.com:
>
> > i think i just need a fresh set of eyes. why would pre-compiling the
> > pattern make this fail?
> >
> > #!/usr/bin/perl
> >
> > use strict; use warnings;
> >
> > my $entry = qr/login\/response/; # prints "did not match.".
> > # my $entry = 'login/response'; # prints "matched."
> >
> > print $entry, "\n";
> >
> > my $record = q{08/16/2006 00:00:01 GET
> > /ftgw/Fas/Fidelity/RtlCust/Login/Response blah blah};
> >
> > if ( $record =~ m/$entry/i ) {
> > print "matched.\n";
> > }

>
> Well, take a look at what gets printed when you print $entry. The i flag
> in the match in the if condition does not affect what's inside the qr.
> That is, the qr version is case sensitive.
>
> Recommend YAPE::Explain.


couldn't find YAPE::Explain on cpan, but I did find
YAPE::Regex::Explain ;-).
Thx A. Sinan.



All times are GMT. The time now is 12:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.