Skip to content

Commit c602faf

Browse files
authored
file-globbing: add docs for ordered pattern matching (#46913)
1 parent 0c43c35 commit c602faf

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/core/extensions/file-globbing.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,37 @@ The preceding C# code:
141141

142142
The additional `Match` overloads work in similar ways.
143143

144+
### Ordered evaluation of include/exclude
145+
146+
By default, the matcher evaluates **all** include patterns first, then applies **all** exclude patterns, regardless of the order in which you added them. This means you can't re-include files that were previously excluded.
147+
148+
Starting in version 10 of the [📦 Microsoft.Extensions.FileSystemGlobbing package](https://www.nuget.org/packages/Microsoft.Extensions.FileSystemGlobbing), you can opt into *ordered* evaluation, where includes and excludes are processed exactly in the sequence they were added:
149+
150+
```csharp
151+
using Microsoft.Extensions.FileSystemGlobbing;
152+
153+
// Preserve the order of patterns when matching.
154+
Matcher matcher = new(preserveFilterOrder: true);
155+
156+
matcher.AddInclude("**/*"); // include everything
157+
matcher.AddExclude("logs/**/*"); // exclude logs
158+
matcher.AddInclude("logs/important/**/*"); // re-include important logs
159+
160+
var result = matcher.Execute(new DirectoryInfoWrapper(new DirectoryInfo(root)));
161+
foreach (var file in result.Files)
162+
{
163+
Console.WriteLine(file.Path);
164+
}
165+
```
166+
167+
In this mode, patterns are applied one after another:
168+
169+
- `**/*` adds all files.
170+
- `logs/**/*` filters out anything in `logs/`.
171+
- `logs/important/**/*` adds back only files under `logs/important/`.
172+
173+
Existing code that uses the default constructor will continue to run with the original "all includes, then all excludes" behavior.
174+
144175
## Pattern formats
145176

146177
The patterns that are specified in the `AddExclude` and `AddInclude` methods can use the following formats to match multiple files or directories.

0 commit comments

Comments
 (0)