Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Issue with regex

by shounak mukherjee (Initiate)
on Jun 23, 2017 at 15:47 UTC ( #1193378=perlquestion: print w/replies, xml ) Need Help??

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

Hello Experts, I am new to perl programming and facing a few issues related to perl regular expressions. I need to apply a condition logic only if my $filename is something like: $filename =._foo.xml #Any file that starts with ._" For such files,I need to overlook the content of the file. while( ($fileName = readdir(DIR))) { next if ($fileName eq "^\._.*"); However I think this regular expresion is not able to filter the filenames based on the criteria. I shall be highly obliged if anyone can please help.

Replies are listed 'Best First'.
Re: Issue with regex
by choroba (Cardinal) on Jun 23, 2017 at 15:50 UTC
    eq is used for string equality. Use the binding operator =~ to bind a regular expression match against a variable:
    $fileName =~ /^\._/;

    BTW, as you can see, there's no need to add .* at the end, as the regex doesn't have to match the whole string.

    See perlop for details.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Issue with regex
by CountZero (Bishop) on Jun 24, 2017 at 09:21 UTC
    File::Find::Rule may be helpful here:

    use Modern::PBP::Perl; use File::Find::Rule; my $path = 'C:\My\Path\to\files'; my $exclude = '^\._'; my $rule = File::Find::Rule->file->not_name(qr/$exclude/)->start($path +); while ( defined ( my $file = $rule->match ) ) { say $file; }


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2023-05-28 02:39 GMT
Find Nodes?
    Voting Booth?

    No recent polls found