简体   繁体   English

有没有办法编写一个匹配所有文件的 glob 模式,除了文件夹中的文件?

[英]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_PATHNAMEFNM_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?*}表示:

  • Any string that doesn't start with f任何不以f开头的字符串
  • The strinf f压力f
  • Any string that starts with f and whose second character is not a o任何以f开头且第二个字符不是o字符串
  • The string fo字符串fo
  • Any string that starts with fo and whose third character is not a o任何以fo开头且第三个字符不是o字符串
  • Any string that starts with foo if it has at least one more character任何以foo开头的字符串,如果它至少还有一个字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM