简体   繁体   English

这两个glob模式之间有什么区别吗?

[英]Is there any difference between these 2 glob patterns?

images/**/*.{png,svg}
images/**/*.+(png|svg)

When I test them they usually produce the same result. 当我测试它们时,它们通常会产生相同的结果。 But just need to make sure they are exactly the same. 但是只需要确保它们完全相同即可。

They are not exactly the same and they will both potentially yield different results - refer to the glob primer for further info. 它们并不完全相同,它们都可能会产生不同的结果-有关更多信息,请参见glob引物 It states: 它指出:

+(pattern|pattern|pattern) Matches one or more occurrences of the patterns provided. +(pattern|pattern|pattern)匹配一个或多个出现的提供的图案。

Your second pattern; 您的第二种模式; images/**/*.+(png|svg) , will match one or more occurrences of either png or svg at the end of a filename. images/**/*.+(png|svg) ,将匹配文件名末尾出现一个或多个 pngsvg

However, your first pattern; 但是,您的第一个模式; images/**/*.{png,svg} , will match only when there is one occurrence of either png or svg at the end of a filename. images/**/*.{png,svg} ,仅在文件名末尾出现 pngsvg时才匹配。

Example: 例:

Let's say we have four files named: 假设我们有四个名为:

  • foo.svg
  • foo.svgsvg
  • foo.png
  • foo.pngpngpng

and their paths are: 它们的路径是:

.
├── images
│   ├── ...
│   └── quux
│       ├── foo.svg
│       ├── foo.svgsvg
│       ├── foo.png
│       └── foo.pngpngpng

Matched Results 匹配结果

Given your two example patterns - your first pattern; 给定您的两个示例模式-您的第一个模式; images/**/*.{png,svg} , will match the following two paths only: images/**/*.{png,svg} ,将仅匹配以下两个路径:

  • images/quux/foo.svg
  • images/quux/foo.png

Note: the paths to files foo.svgsvg and foo.pngpngpng have been ignored 注意:文件foo.svgsvgfoo.pngpngpng的路径已被忽略

However your second pattern; 但是你的第二种模式; images/**/*.+(png|svg) will match all four file paths, namely: images/**/*.+(png|svg)将匹配所有四个文件路径,即:

  • images/quux/foo.svg
  • images/quux/foo.svgsvg
  • images/quux/foo.png
  • images/quux/foo.pngpngpng

"When I test them they usually produce the same result." “当我测试它们时,它们通常会产生相同的结果。”

This is because it's unlikely that you've ever had a file with an extension such as .pngpng or .svgsvgsvg (ie when it has more than one occurrence of png or svg at the end). 这是因为您不太可能拥有一个扩展名为.pngpng.svgsvgsvg (即,当结尾处出现多个pngsvg时)。 Nor is it likely that you ever will. 您也永远不会。

However, I recommend that you utilize the first glob pattern, ie images/**/*.{png,svg} , as it's more concise to what you're actually wanting to match. 但是,我建议您使用第一个glob模式,即images/**/*.{png,svg} ,因为它对您实际想要匹配的内容更加简洁。

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

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