Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How to get an exact match of files

by Anonymous Monk
on Mar 24, 2011 at 05:15 UTC ( [id://895181]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to get an exact match of $file with $data.I am using the below if condition,but that doesnt seem to match and it matches other data aswell?How to get an exact match?I only want to match "adi"

$data="Adi";

$file="$(ROOT)/../../../../../../../Audio_Driver/adie_lib/$(CPU)/adi"

How to get an exact match of $data
if ($file =~ /^\/\Q$data\E$/i) { }

Replies are listed 'Best First'.
Re: How to get an exact match of files
by merlyn (Sage) on Mar 24, 2011 at 05:18 UTC
    I think you're looking for "eq", as in $date eq $file.

    However, if you insist using a regex, please reveal the actual context, as it is probably not freeform Perl code then.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

      $data="Adi";

      $file="$(ROOT)/../../../../../../../Audio_Driver/adie_lib/$(CPU)/adi"

      For the above data if I use $data eq $file,will they be equal?is it not case-sensitive?how to make the eq case-insensitive?

Re: How to get an exact match of files
by Nikhil Jain (Monk) on Mar 24, 2011 at 05:42 UTC

    I think you are trying to parse a file path, if so i would recommend you to see a module File::Basename - Parse file paths into directory, filename and suffix.

    If you really want regex to match exact word then try something like

    use strict; my $data="Adi"; my $file="$(ROOT)/../../../../adi/../../Audio_Driver/adie_lib/$(CPU)/a +di" ; if($file =~ /\b$data\b$/i){ # apply word boundaries by \b print "match\n"; }

    One more thing The \Q tells Perl where to start escaping special characters, and the \E tells it where to stop, and in your given $data variable, i am not finding any special characters so no need to use it.

    It would be nice if you also see perlfaq6.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://895181]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-03-29 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found