Ramstack.Globbing
1.0.0
See the version list below for details.
dotnet add package Ramstack.Globbing --version 1.0.0
NuGet\Install-Package Ramstack.Globbing -Version 1.0.0
<PackageReference Include="Ramstack.Globbing" Version="1.0.0" />
paket add Ramstack.Globbing --version 1.0.0
#r "nuget: Ramstack.Globbing, 1.0.0"
// Install Ramstack.Globbing as a Cake Addin #addin nuget:?package=Ramstack.Globbing&version=1.0.0 // Install Ramstack.Globbing as a Cake Tool #tool nuget:?package=Ramstack.Globbing&version=1.0.0
Ramstack.Globbing
Fast and zero-allocation .NET globbing library for matching file paths using glob patterns. No external dependencies.
Getting Started
To install the Ramstack.Globbing
NuGet package to your project, run the following command:
dotnet add package Ramstack.Globbing
Usage
bool result = Matcher.IsMatch("wiki/**/*.md", "wiki/section-1/start.md");
The IsMatch
method attempts to match the specified path against the provided wildcard pattern.
By default, the system's default path separators are used. You can override this behavior by specifying one of the following flags:
Name | Description |
---|---|
Auto | Automatically determines whether to treat backslashes (\\ ) as escape sequences or path separators based on the platform's separator convention. |
Windows | Treats backslashes (\\ ) as path separators instead of escape sequences.<br>Provides behavior consistent with Windows-style paths.<br>Both backslashes (\\ ) and forward slashes (/ ) are considered as path separators in this mode. |
Unix | Treats backslashes (\\ ) as escape sequences, allowing for special character escaping.<br>Provides behavior consistent with Unix-style paths. |
Example with a specific flag:
bool result = Matcher.IsMatch(@"wiki\**\*.md", "wiki/section-1/start.md", MatchFlags.Windows);
Patterns
From Wikipedia
Pattern | Description | Example | Matches | Does not match |
---|---|---|---|---|
* | matches any number of any characters including none | Law* | Law, Laws, or Lawyer | GrokLaw, La, Law/foo or aw |
*Law* | Law, GrokLaw, or Lawyer. | La, or aw | ||
? | matches any single character | ?at | Cat, cat, Bat or bat | at |
[abc] | matches one character given in the bracket | [CB]at | Cat or Bat | cat, bat or CBat |
[a-z] | matches one character from the (locale-dependent) range given in the bracket | Letter[0-9] | Letter0, Letter1, Letter2 up to Letter9 | Letters, Letter or Letter10 |
[!abc] | matches one character that is not given in the bracket | [!C]at | Bat, bat, or cat | Cat |
[!a-z] | matches one character that is not from the range given in the bracket | Letter[!3-5] | Letter1, Letter2, Letter6 up to Letter9 and Letterx etc. | Letter3, Letter4, Letter5 or Letterxx |
Pattern specific for directories
Pattern | Description | Example | Matches | Does not match |
---|---|---|---|---|
** | matches any number of path segments including none | **/Law | dir1/dir2/Law, dir1/Law or Law | dir1/La |
Brace patterns
Brace patterns allow for matching multiple alternatives in a single pattern. Here are some key features:
Pattern | Description | Example | Matches | Does not match |
---|---|---|---|---|
{a,b,c} | matches any of the comma-separated terms | file.{jpg,png} | file.jpg, file.png | file.gif |
{src,test{s,}} | supports nested brace patterns | {src,test{s,}}/*.cs | src/main.cs, tests/unit.cs, test/integration.cs | doc/readme.cs |
{src,,test} | supports empty alternatives | {src,,test}/file.txt | src/file.txt, file.txt, test/file.txt | doc/file.txt |
{[sS]rc,test*} | supports full glob pattern within braces | {[sS]rc,test*}/*.cs | src/app.cs, Src/main.cs, testing/script.cs | lib/util.cs |
- Empty alternatives are valid, e.g.,
{src,test,}
will also match paths without the listed prefixes. - Brace patterns can be nested, allowing for complex matching scenarios.
- Full glob patterns can be used within braces, providing powerful and flexible matching capabilities.
Escaping characters
The meta characters ?
, *
, [
, \
can be escaped by using the []
, which means match one character listed in the bracket.
[[]
matches the literal[
[*]
matches the literal*
This works when using any MatchFlags
(Windows
or Unix
).
When using MatchFlags.Unix
, an additional escape character (\
) is available:
\[
matches the literal[
\*
matches the literal*
Notes
- Leading and trailing path separators are ignored.
- Consecutive path separators are counted as one separator.
Special cases
- At the root level, an empty path segment is valid, which can be represented by patterns like "*".
- At any deeper level, an empty segment indicates that a required directory or file is missing, making the path invalid for patterns expecting something at that level.
Pattern | Matches | Does not match | Explanation |
---|---|---|---|
* |
foo , "" |
Matches everything, e.g. empty string | |
*/* |
a/b ,b/c |
a ,b ,foo |
Requires at least one directory level, so a is not a match |
*/{,b} |
a/b |
a ,b ,foo |
Requires a directory or a specific file b at the next level, so a doesn't match |
💡 This means that the patterns */{}
and */{,}
cannot match any path due to the rule: an empty segment is not allowed beyond the root level.
Optimizations
We use optimizations that prevent quadratic behavior in scenarios like the pattern a*a*a*a*a*a*a*a*a*c
matching against the text aaaaaaaaaaaaaaa...aaaa...aaa
.
Similarly, for the a/**/a/**/a/**/.../a/**/a/**/a/**/b
pattern matching against a/a/a/a/.../a/.../a
.
Supported versions
Version | |
---|---|
.NET | 6, 7, 8 |
Contributions
Bug reports and contributions are welcome.
License
This package is released as open source under the MIT License. See the LICENSE file for more details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Ramstack.Globbing:
Package | Downloads |
---|---|
Ramstack.FileSystem.Abstractions
A .NET library that provides a virtual file system abstraction. |
|
Ramstack.FileSystem.Globbing
Provides an implementation of Ramstack.FileSystem that applies glob-based filtering rules to the underlying file system. |
|
Ramstack.FileProviders.Globbing
A .NET library implementing a IFileProvider that filters files using include and/or exclude glob patterns. |
|
Ramstack.FileProviders.Extensions
A lightweight .NET library of useful and convenient extensions for Microsoft.Extensions.FileProviders. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.2.0 | 921 | 8/26/2024 |
2.2.0-preview.3 | 663 | 8/22/2024 |
2.2.0-preview.2 | 83 | 8/21/2024 |
2.2.0-preview.1 | 55 | 8/21/2024 |
2.1.0 | 438 | 8/3/2024 |
2.1.0-preview.7 | 70 | 7/31/2024 |
2.1.0-preview.6 | 57 | 7/28/2024 |
2.1.0-preview.5 | 56 | 7/26/2024 |
2.1.0-preview.4 | 55 | 7/22/2024 |
2.1.0-preview.3 | 66 | 7/21/2024 |
2.1.0-preview.2 | 58 | 7/17/2024 |
2.1.0-preview.1 | 47 | 7/17/2024 |
2.0.0 | 93 | 7/15/2024 |
1.1.0 | 95 | 7/10/2024 |
1.0.0 | 106 | 7/9/2024 |