aeqr has asked for the wisdom of the Perl Monks concerning the following question:
What I try here is "all files that don't match something with .pl at the end". But it still prints everything. So I have tried something like this:sub list_files{ opendir(my $dir,".") or die "Could not open current directory"; while (my $file = readdir($dir)) { print "$file\n" if ($file=~m/(?!\.pl$)/); } closedir $dir; }
The second one does what I want. Both seem to be similar however they don't behave the same. Why?sub list_files{ opendir(my $dir,".") or die "Could not open current directory"; while (my $file = readdir($dir)) { print "$file\n" if (!($file=~m/(?=\.pl$)/)); } closedir $dir; }
Additional question: The scripts prints also "." and "..", why are they and can it be avoid? I want to make some changes in the file names that are in the current directory. But not on "." and ".."
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex negative lookahead
by NetWallah (Canon) on Apr 25, 2014 at 04:20 UTC | |
by aeqr (Novice) on Apr 25, 2014 at 04:35 UTC | |
by NetWallah (Canon) on Apr 25, 2014 at 04:39 UTC | |
by mr_mischief (Monsignor) on Apr 25, 2014 at 14:16 UTC | |
|
Re: Regex negative lookahead
by thargas (Deacon) on Apr 25, 2014 at 13:54 UTC | |
|
Re: Regex negative lookahead
by mr_mischief (Monsignor) on Apr 25, 2014 at 14:57 UTC | |
|
Re: Regex negative lookahead
by AnomalousMonk (Archbishop) on Apr 25, 2014 at 17:23 UTC | |
|
Re: Regex negative lookahead
by pvaldes (Chaplain) on Apr 26, 2014 at 10:02 UTC | |
by Anonymous Monk on Apr 26, 2014 at 10:24 UTC |