regex - Excluding a file with perl grep -
i want go on of files in directory, except files ending '.py'. line in existing script is:
my @files = sort(grep(!/^(\.|\.\.)$/, readdir($dir_h)));
and want like:
my @files = sort(grep(!/^(\.|\.\.|"*.py")$/, readdir($dir_h)));
can please exact syntax?
grep uses regular expressions, not globs (aka wildcards). correct syntax is
my @files = sort(grep(!/^(\.|\.\.|.*\.py)$/, readdir($dir_h)));
or, without unnecessary parentheses
my @files = sort grep ! /^(\.|\.\.|.*\.py)$/, readdir $dir_h;
as parentheses in regular expression aren't used capturing, precedence, can change them non-capturing:
my @files = sort grep ! /^(?:\.|\.\.|.*\.py)$/, readdir $dir_h;
you can express same in many different ways, e.g.
/^\.{1,2}$|\.py$/
i.e. dot once or twice nothing around, or .py
@ end.
Comments
Post a Comment