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

  2. it_says_BALLS_on_your forehead

    Paul Lalli Guest

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

  3. it_says_BALLS_on_your forehead

    Ben Morrow Guest

    If you use different delimiters you don't need the \.
    If you set $\ Perl will print the "\n" for you.
    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
     
    Ben Morrow, Aug 17, 2006
    #3

  4. ahh, duh! thx Paul.
     
    it_says_BALLS_on_your forehead, Aug 17, 2006
    #4
  5. 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. Advertisements

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 (here). After that, you can post your question and our members will help you out.