devlele has asked for the wisdom of the Perl Monks concerning the following question:



Hi all,
I have a variable $match_dir which has the value c:\abc and I have an array @subdirs_current_dir in which I have directories such as c:\abc123, c:\def, c:\abctyp etc. Now I want to retrieve c:\abc123 and c:\abctyp from the array. So I wrote following piece of code
foreach(@subdirs_current_dir){if($_ =~ /^($match_dir)/){print "matche +d ...;}}
In this code, I am not getting any match. Does anyone know why
Please help ..
Thanks.

Replies are listed 'Best First'.
Re: pattern matching question
by Roy Johnson (Monsignor) on Jan 26, 2006 at 21:31 UTC
    Because your backslash is being interpreted by the regex. Try
    if($_ =~ /^(\Q$match_dir\E)/

    Caution: Contents may have been coded under pressure.
Re: pattern matching question
by Fletch (Bishop) on Jan 26, 2006 at 21:30 UTC

    The \ character is probably being eaten by the regex engine. See quotemeta.