Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > qr makes pattern match fail

Reply
Thread Tools

qr makes pattern match fail

 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      08-17-2006
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";
}

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-17-2006
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

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      08-17-2006

Quoth "it_says_BALLS_on_your forehead" <(E-Mail Removed)>:
> 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".
[(E-Mail Removed)]
 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      08-17-2006

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.

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      08-17-2006

A. Sinan Unur wrote:
> "it_says_BALLS_on_your forehead" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pattern match to fail if two periods in a row Ralph Shnelvar Ruby 9 11-28-2009 05:40 AM
net/ssh makes log4r fail Manish Sapariya Ruby 0 10-19-2007 08:54 AM
Timeout makes report fail Morten Snedker ASP .Net 2 02-01-2007 06:44 AM
what's the design pattern for trial-fail-modify-retry John Black C++ 1 03-07-2004 10:16 PM
if (f() != FAIL) or if (FAIL != f())? Wenjie C Programming 3 07-31-2003 09:54 PM



Advertisments