qr makes pattern match fail

Discussion in 'Perl Misc' started by it_says_BALLS_on_your forehead, Aug 17, 2006.

  1. 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";
    }
     
    it_says_BALLS_on_your forehead, Aug 17, 2006
    #1
    1. Advertising

  2. it_says_BALLS_on_your forehead

    Paul Lalli Guest

    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
     
    Paul Lalli, Aug 17, 2006
    #2
    1. Advertising

  3. it_says_BALLS_on_your forehead

    Ben Morrow Guest

    Quoth "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.".


    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".
    []
     
    Ben Morrow, Aug 17, 2006
    #3
  4. 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, Aug 17, 2006
    #4
  5. A. Sinan Unur wrote:
    > "it_says_BALLS_on_your forehead" <> wrote in
    > news::
    >
    > > 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.
     
    it_says_BALLS_on_your forehead, Aug 17, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Black
    Replies:
    1
    Views:
    567
    H. S. Lahman
    Mar 7, 2004
  2. Wenjie

    if (f() != FAIL) or if (FAIL != f())?

    Wenjie, Jul 28, 2003, in forum: C Programming
    Replies:
    3
    Views:
    458
    E. Robert Tisdale
    Jul 31, 2003
  3. Morten Snedker

    Timeout makes report fail

    Morten Snedker, Jan 31, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    586
    Morten Snedker
    Feb 1, 2007
  4. Manish Sapariya

    net/ssh makes log4r fail

    Manish Sapariya, Oct 19, 2007, in forum: Ruby
    Replies:
    0
    Views:
    100
    Manish Sapariya
    Oct 19, 2007
  5. Ralph Shnelvar
    Replies:
    9
    Views:
    113
    Steve Wilhelm
    Nov 28, 2009
Loading...

Share This Page