how to resolve a variable but disallow interpolation in regex match

P

Praveen Kallakuri

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?
 
B

Brad Baxter

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
 
M

Matthew Braid

Praveen said:
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
 
P

Praveen Kallakuri

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.
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top