Skip to content

file-globbing: add docs for ordered pattern matching #46913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/core/extensions/file-globbing.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,36 @@ The preceding C# code:

The additional `Match` overloads work in similar ways.

### Ordered evaluation of include/exclude

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 cannot re-include files that were previously excluded.

Starting with version 10, you can opt into *ordered* evaluation so that includes and excludes are processed exactly in the sequence they were added:

```csharp
using Microsoft.Extensions.FileSystemGlobbing;

// preserve the order of patterns when matching
Matcher matcher = new(preserveFilterOrder: true);

matcher.AddInclude("**/*"); // include everything
matcher.AddExclude("logs/**/*"); // exclude logs
matcher.AddInclude("logs/important/**/*"); // re-include important logs

var result = matcher.Execute(new DirectoryInfoWrapper(new DirectoryInfo(root)));
foreach (var file in result.Files)
{
Console.WriteLine(file.Path);
}
```

In this mode, patterns are applied one after another:
- `**/*` adds all files
- `logs/**/*` filters out anything in `logs/`
- `logs/important/**/*` adds back only files under `logs/important/`

Existing code that uses the default constructor will continue to run with the original “all includes, then all excludes” behavior.

## Pattern formats

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