Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to resolve a variable but disallow interpolation in regex match

Reply
Thread Tools

how to resolve a variable but disallow interpolation in regex match

 
 
Praveen Kallakuri
Guest
Posts: n/a
 
      04-15-2004
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?
 
Reply With Quote
 
 
 
 
Brad Baxter
Guest
Posts: n/a
 
      04-15-2004
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
 
Reply With Quote
 
 
 
 
Matthew Braid
Guest
Posts: n/a
 
      04-15-2004
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

 
Reply With Quote
 
Praveen Kallakuri
Guest
Posts: n/a
 
      04-15-2004
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.

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
src-resolve: Cannot resolve the name ... ivanet@gmail.com XML 1 03-23-2007 12:10 PM
Variable Interpolation with %%variable phrankster Perl Misc 9 08-14-2005 12:19 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments