Ramstack.Globbing 2.1.0-preview.3

This is a prerelease version of Ramstack.Globbing.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Ramstack.Globbing --version 2.1.0-preview.3                
NuGet\Install-Package Ramstack.Globbing -Version 2.1.0-preview.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ramstack.Globbing" Version="2.1.0-preview.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ramstack.Globbing --version 2.1.0-preview.3                
#r "nuget: Ramstack.Globbing, 2.1.0-preview.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Ramstack.Globbing as a Cake Addin
#addin nuget:?package=Ramstack.Globbing&version=2.1.0-preview.3&prerelease

// Install Ramstack.Globbing as a Cake Tool
#tool nuget:?package=Ramstack.Globbing&version=2.1.0-preview.3&prerelease                

Ramstack.Globbing

Fast and zero-allocation .NET globbing library for matching file paths using glob patterns. No external dependencies.

.NET

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/section-1/start.md", "wiki/**/*.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/section-1/start.md", @"wiki\**\*.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
{main,,test} supports empty alternatives {main,,test}1.txt main1.txt, test1.txt, 1.txt file1.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.

File traversal

The Files class provides functionality for traversing the file system and retrieving lists of files and directories based on specified glob patterns. This allows for flexible and efficient file and directory enumeration.

using Ramstack.Globbing.Traversal;

// List all *.cs files
var files = Files.EnumerateFiles(@"/path/to/directory", "**/*.cs");
foreach (var file in files)
    Console.WriteLine(file);

// List all *.cs files except in tests directory
var files = Files.EnumerateFiles(@"/path/to/directory", "**/*.cs", "tests");
foreach (var file in files)
    Console.WriteLine(file);

Support for multiple patterns is also included:

using Ramstack.Globbing.Traversal;

// List all *.cs files
var files = Files.EnumerateFiles(@"/path/to/directory", ["src/**/*.cs", "lib/**/*.cs"], ["**/tests"]);
foreach (var file in files)
    Console.WriteLine(file);

Changelog

2.0.0

  • Added the ability to retrieve a list of files and directories based on a specified glob pattern.

BREAKING CHANGE

To improve code readability and adherence to .NET conventions, the order of parameters in the IsMatch method has been changed. The path parameter is now first, followed by the pattern parameter.

New signature

public static bool IsMatch(string path, string pattern, MatchFlags flags = MatchFlags.Auto)

Reasons for the change:

  • Consistency with .NET methods: The primary object of an action is typically the first parameter. Here, path is the primary object, and pattern is the secondary.
  • Alignment with common APIs: This order matches other .NET methods like Regex.IsMatch(string input, string pattern).
  • Flexibility for future expansions: Having the most commonly varied parameter (pattern) second makes it easier to create intuitive method overloads in the future.
  • Early stage of development: Since the library is newly released, we've made this change to ensure consistency from the start.

1.1.0

  • Change target framework from multi-targeting (net6.0;net8.0) to single target net6.0
  • Replace conditional compilation for .NET 8 with a universal approach

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 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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.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 940 8/26/2024
2.2.0-preview.3 669 8/22/2024
2.2.0-preview.2 89 8/21/2024
2.2.0-preview.1 59 8/21/2024
2.1.0 445 8/3/2024
2.1.0-preview.7 76 7/31/2024
2.1.0-preview.6 63 7/28/2024
2.1.0-preview.5 60 7/26/2024
2.1.0-preview.4 58 7/22/2024
2.1.0-preview.3 71 7/21/2024
2.1.0-preview.2 61 7/17/2024
2.1.0-preview.1 52 7/17/2024
2.0.0 100 7/15/2024
1.1.0 108 7/10/2024
1.0.0 115 7/9/2024