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

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -