For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2004 > how to pattern match \d in a variable?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to pattern match \d in a variable?
nickelstat

2004-03-29, 6:50 pm

I'm at a complete loss:
$value = "ITEM_001_in_001.ldg";
$pattern = '_in_\d\d\d.ldg'; # a substring of $value

print "pattern($pattern)
Value($value)\n";

#if ($value !~ /_in_\d\d\d.ldg/) -> this works, but not line below
if ($value !~ /\Q$pattern/) {
print "patern not found\n";
} else { print "OK OK OK\n"; }

I get "pattern not found" instead of OK OK OK.
pattern(_in_\d\d\d.ldg)
Value(ITEM_001_in_001.ldg)
patern not found
Gunnar Hjalmarsson

2004-03-29, 6:50 pm

nickelstat wrote:
>
> $value = "ITEM_001_in_001.ldg";
> $pattern = '_in_\d\d\d.ldg'; # a substring of $value


Suppose you mean

$pattern = '_in_\d\d\d\.ldg';
--------------------------^

> if ($value !~ /\Q$pattern/) {

-----------------^^
What made you believe you should quote the meta characters? You shouldn't.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Joe Smith

2004-03-31, 6:33 am

nickelstat wrote:

> I'm at a complete loss:
> $value = "ITEM_001_in_001.ldg";
> $pattern = '_in_\d\d\d.ldg'; # a substring of $value


I'd recommend using qr() when dealing with regular expressions.
$pattern = qr/_in_\d\d\d\.ldg/; # Note \.

> print "pattern($pattern)
> Value($value)\n";
>
> #if ($value !~ /_in_\d\d\d.ldg/) -> this works, but not line below
> if ($value !~ /\Q$pattern/) {
> print "patern not found\n";
> } else { print "OK OK OK\n"; }


Get rid of that silly \Q and it will work.
-Joe
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com