qr makes pattern match fail

  • Thread starter it_says_BALLS_on_your forehead
  • Start date
I

it_says_BALLS_on_your forehead

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";
}
 
P

Paul Lalli

it_says_BALLS_on_your forehead said:
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
 
B

Ben Morrow

Quoth "it_says_BALLS_on_your forehead said:
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

it_says_BALLS_on_your forehead

Paul said:
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.
 
I

it_says_BALLS_on_your forehead

A. Sinan Unur said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top