Even with the \Q...\E your regular expression may not be doing what you want.

$filename='Z:\repo\bin\tools\scripts\shared\users\script.pl'; $key =~ /\Q$filename\E/i; #matches (notice different starts and ends) Z:\repo\bin\tools\scripts\shared\users\script.pl Z:\repo\bin\tools\scripts\shared\users\script.plm Z:\repo\bin\tools\scripts\shared\users\script.plx Z:\repo\bin\tools\scripts\shared\users\script.pl\foobar \\?\Z:\repo\bin\tools\scripts\shared\users\script.plm # and does NOT match Z:/repo/bin/tools/scripts/shared/users/script.pl

The extra matches come because /\Q$filename\E can match anywhere within a string, not just beginning to end. To match the whole string you need to start with a caret and end with a dollar sign: /^\Q$filename\E$/. However if you are matching the whole string and using quotemeta (\Q...\E) you might as well just do lc $key $eq lc $filename. lc makes both sides of the equality lower case, making case irrelevant just as the i flag on the regex does.

The missed matches come from the MS-Windows operating systems having several different ways of expressing the exact same path. Making your regex case insensitive may not be enough to get all the matches you want.

I forget exactly when MS started recognizing '/' in paths, but certainly XP onward, you can use either '/' or '\' to separate path segments. One solution to that is to preprocess your paths by substituting '\' for '/', e.g. grep { lc $_ eq lc $filename } map { s/\//\\/g; $_ } @paths or grep { my $x=$_;$x =~ s/\//\\/g; lc $x eq lc $filename } @paths if you want to preserve the original paths in your output array.

Update: added alternative that preserves original paths.


In reply to Re: How to grep for a filename for each of the keys of a hash by ELISHEVA
in thread How to grep for a filename for each of the keys of a hash by perl_mystery

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.