[英]Is there a way to write a glob pattern that matches all files except those in a folder?
I need to write a file glob that will match all files except for those that are contained within a certain folder (eg all files except those contained within the high level folder foo/
.我需要编写一个文件 glob 来匹配除某个文件夹中包含的文件之外的所有文件(例如,除高级文件夹foo/
包含的文件之外的所有文件。
I've arrived at the following glob:我已经到达了以下全局:
!(foo)/**/*
However, this glob doesn't seem to match on any files in Ruby's File.fnmatch
(even with FNM_PATHNAME
and FNM_DOTMATCH
set to true
.但是,这个 glob 似乎与 Ruby 的File.fnmatch
任何文件都不匹配(即使FNM_PATHNAME
和FNM_DOTMATCH
设置为true
。
Also, Ruby's glob interpreter seemed to have different behavior than JavaScript's:此外,Ruby 的 glob 解释器似乎与 JavaScript 的行为不同:
JavaScript glob interpreter matches all strings JavaScript glob 解释器匹配所有字符串
Ruby glob interpreter doesn't match any strings: Ruby glob 解释器不匹配任何字符串:
2.6.2 :027 > File.fnmatch("!(foo)/**/*", "hello/world.js")
=> false
2.6.2 :028 > File.fnmatch("!(foo)/**/*", "test/some/globs")
=> false
2.6.2 :029 > File.fnmatch("!(foo)/**/*", "foo/bar/something.txt")
=> false
If you really need to use a glob then you can list what is allowed, making it equivalent to the negation:如果您确实需要使用 glob,那么您可以列出允许的内容,使其等效于否定:
extglob = "{[^f]*,f,f[^o]*,fo,fo[^o]*,foo?*}/**/*"
File.fnmatch(extglob, "hello/world.js", File::FNM_EXTGLOB | File::FNM_PATHNAME)
#=> true
File.fnmatch(extglob, "test/some/globs", File::FNM_EXTGLOB | File::FNM_PATHNAME)
#=> true
File.fnmatch(extglob, "foo/bar/something.txt", File::FNM_EXTGLOB | File::FNM_PATHNAME)
#=> false
File.fnmatch(extglob, "food/bar/something.txt", File::FNM_EXTGLOB | File::FNM_PATHNAME)
#=> true
{[^f]*,f,f[^o]*,fo,fo[^o]*,foo?*}
means: {[^f]*,f,f[^o]*,fo,fo[^o]*,foo?*}
表示:
f
任何不以f
开头的字符串f
压力f
f
and whose second character is not a o
任何以f
开头且第二个字符不是o
字符串fo
字符串fo
fo
and whose third character is not a o
任何以fo
开头且第三个字符不是o
字符串foo
if it has at least one more character任何以foo
开头的字符串,如果它至少还有一个字符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.