how to resolve a variable but disallow interpolation in regex match

Discussion in 'Perl Misc' started by Praveen Kallakuri, Apr 15, 2004.

  1. hi-

    one of the lines i am reading from a file contains the following text:

    2004-04-13 14:03:06,502 [Thread-1 2] | ServiceCache: adding
    'lms.createCertificate'.

    i read this line along with others in the file into a list. problem is,
    whenever a list element containing this line is used as part of a regex,
    perl bombs with:

    Invalid [] range "d-1" in regex; marked by <-- HERE in m/2004-04-13
    14:03:06,502 [Thread-1 <-- HERE 2] | ServiceCache: adding
    'lms.createCertificate'./ at spec_re.pl line 11, <FD> line 1.

    it seems that "Thread-1 is being considered as a range in []. i wrote a
    smaller test program that can simulate this error and this is how it
    looks:

    while (<FD>) {
    chomp;
    if ( $_ !~ $_ ) {
    print "this should never print\n";
    }
    }

    perdiag, perlre, or perlop did not help me find a way around this error.
    (or i missed it even if it was there.) can someone tell me how i can tell
    perl to simply resolve the variable "$_" but not interpolate it during a
    match?
     
    Praveen Kallakuri, Apr 15, 2004
    #1
    1. Advertising

  2. Praveen Kallakuri

    Brad Baxter Guest

    Re: how to resolve a variable but disallow interpolation in regexmatch

    On Wed, 14 Apr 2004, Praveen Kallakuri wrote:

    > it seems that "Thread-1 is being considered as a range in []. i wrote a
    > smaller test program that can simulate this error and this is how it
    > looks:
    >
    > while (<FD>) {
    > chomp;
    > if ( $_ !~ $_ ) {
    > print "this should never print\n";
    > }
    > }
    >
    > perdiag, perlre, or perlop did not help me find a way around this error.
    > (or i missed it even if it was there.) can someone tell me how i can tell
    > perl to simply resolve the variable "$_" but not interpolate it during a
    > match?
    >


    \Q (perlre), as in: if ( $_ !~ "\Q$_" ) {

    Regards,

    Brad
     
    Brad Baxter, Apr 15, 2004
    #2
    1. Advertising

  3. Re: how to resolve a variable but disallow interpolation in regexmatch

    Praveen Kallakuri wrote:

    > hi-
    >
    > one of the lines i am reading from a file contains the following text:
    >
    > 2004-04-13 14:03:06,502 [Thread-1 2] | ServiceCache: adding
    > 'lms.createCertificate'.
    >
    > i read this line along with others in the file into a list. problem is,
    > whenever a list element containing this line is used as part of a regex,
    > perl bombs with:
    >
    > Invalid [] range "d-1" in regex; marked by <-- HERE in m/2004-04-13
    > 14:03:06,502 [Thread-1 <-- HERE 2] | ServiceCache: adding
    > 'lms.createCertificate'./ at spec_re.pl line 11, <FD> line 1.
    >
    > it seems that "Thread-1 is being considered as a range in []. i wrote a
    > smaller test program that can simulate this error and this is how it
    > looks:
    >
    > while (<FD>) {
    > chomp;
    > if ( $_ !~ $_ ) {
    > print "this should never print\n";
    > }
    > }
    >
    > perdiag, perlre, or perlop did not help me find a way around this error.
    > (or i missed it even if it was there.) can someone tell me how i can tell
    > perl to simply resolve the variable "$_" but not interpolate it during a
    > match?


    my $re_breaker = "[Thread-1]";
    my $check = "THIS CONTAINS [Thread-1] !!!";
    print "OK!\n" if $check =~ /\Q$re_breaker\E !!!\z/;
    # ^^ ^^

    \Q quotes everything up to the end of the RE or until a \E is found. The quote
    range is worked out at compile time so you don't have to worry about $re_breaker
    containing \E.

    You can also use quotemeta:

    my $re_breaker = quotemeta("[Thread-1]");
    my $check = "THIS CONTAINS [Thread-1] !!!";
    print "OK!\n" if $check =~ /$re_breaker !!!\z/;

    MB
     
    Matthew Braid, Apr 15, 2004
    #3
  4. On Wed, 14 Apr 2004 21:44:32 -0400, Brad Baxter wrote:

    > On Wed, 14 Apr 2004, Praveen Kallakuri wrote:
    >
    >> it seems that "Thread-1 is being considered as a range in []. i wrote a
    >> smaller test program that can simulate this error and this is how it
    >> looks:
    >>
    >> while (<FD>) {
    >> chomp;
    >> if ( $_ !~ $_ ) {
    >> print "this should never print\n";
    >> }
    >> }
    >>
    >> perdiag, perlre, or perlop did not help me find a way around this error.
    >> (or i missed it even if it was there.) can someone tell me how i can tell
    >> perl to simply resolve the variable "$_" but not interpolate it during a
    >> match?
    >>

    >
    > \Q (perlre), as in: if ( $_ !~ "\Q$_" ) {
    >
    > Regards,
    >
    > Brad



    aah
    that was simple. thanks brad.
     
    Praveen Kallakuri, Apr 15, 2004
    #4
    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. Rich Wallace

    DataGrid - Disallow Postback?

    Rich Wallace, Jul 15, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    527
    Rich Wallace
    Jul 15, 2004
  2. hiwa
    Replies:
    0
    Views:
    653
  3. Replies:
    1
    Views:
    10,314
  4. Replies:
    3
    Views:
    809
    Reedick, Andrew
    Jul 1, 2008
  5. phrankster

    Variable Interpolation with %%variable

    phrankster, Aug 9, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    151
    James Taylor
    Aug 14, 2005
Loading...

Share This Page