DotNet.Glob 2.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package DotNet.Glob --version 2.0.3
NuGet\Install-Package DotNet.Glob -Version 2.0.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="DotNet.Glob" Version="2.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotNet.Glob --version 2.0.3
#r "nuget: DotNet.Glob, 2.0.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 DotNet.Glob as a Cake Addin
#addin nuget:?package=DotNet.Glob&version=2.0.3

// Install DotNet.Glob as a Cake Tool
#tool nuget:?package=DotNet.Glob&version=2.0.3

DotNet.Glob

A fast (probably the fastest) globbing library for .NET.

Branch Build Status NuGet
Master Build master NuGet
Develop Build status NuGet

This library does not use Regex - I wanted to make something much faster. The latest benchmarks show that DotNet.Glob signficantly outperforms Regex. The benchmarks use BenchmarkDotNet and can be located inside this repo. Just dotnet run them. Some Benchmark results have also been published on the wiki: https://github.com/dazinator/DotNet.Glob/wiki/Benchmarks-(vs-Compiled-Regex)

Usage

  1. Install the NuGet package. Install-Package DotNet.Glob
  2. Add using statement: using DotNet.Globbing;
  3. Parse a glob from a pattern
 var glob = Glob.Parse("p?th/*a[bcd]b[e-g]a[1-4][!wxyz][!a-c][!1-3].*");
 var isMatch = glob.IsMatch("pAth/fooooacbfa2vd4.txt");

Build a glob fluently

You can also use the GlobBuilder class if you wish to build up a glob using a fluent syntax. This is also more efficient as it avoids having to parse the glob from a string pattern.

So to build the following glob pattern: /foo?\\*[abc][!1-3].txt:


  var glob = new GlobBuilder()
                .PathSeperator()
                .Literal("foo")
                .AnyCharacter()
                .PathSeperator(PathSeperatorKind.BackwardSlash)
                .Wildcard()
                .OneOf('a', 'b', 'c')
                .NumberNotInRange('1', '3')
                .Literal(".txt")
                .ToGlob();

   var isMatch = glob.IsMatch(@"/fooa\\barrra4.txt"); // returns true.

Patterns

The following patterns are supported (from wikipedia):

Wildcard Description Example Matches Does not match
* matches any number of any characters including none Law* Law, Laws, or Lawyer
? 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 or bat
[a-z] matches one character from the 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

In addition, DotNet Glob also supports:

Wildcard Description Example Matches Does not match
** matches any number of path / directory segments. When used must be the only contents of a segment. /**/some.* /foo/bar/bah/some.txt, /some.txt, or /foo/some.txt

Advanced Usages

Parsing options.

By default, when your glob pattern is parsed, DotNet.Glob will only allow literals which are valid for path / directory names. These are:

  1. Any Letter (A-Z, a-z) or Digit
  2. ., , !, #, -, ;, =, @, ~, _, :

This is optimised for matching against paths / directory strings. However, introduced in version 1.6.4, you can override this behaviour so that you can include arbitrary characters in your literals. For example, here is a pattern that matches the literal "Stuff:

    // Overide the default options globally for all matche:
    GlobParseOptions.Default.Parsing.AllowInvalidPathCharacters = true;
    DotNet.Globbing.Glob.Parse("\"Stuff*").IsMatch("\"Stuff"); // true;    

You can also just set these options on a per glob pattern basis:

    GlobOptions options = new GlobOptions();
    options.Parsing.AllowInvalidPathCharacters = allowInvalidPathCharcters;
    DotNet.Globbing.Glob.Parse("\"Stuff*", globParseOptions).IsMatch("\"Stuff"); // true; 

Case Sensitivity (Available as of version >= 2.0.0)

By default, evaluation is case-sensitive unless you specify otherwise.

    GlobOptions options = new GlobOptions();
    options.Evaluation.CaseInsensitive = true;
    DotNet.Globbing.Glob.Parse("foo*", globParseOptions).IsMatch("FOo"); // true; 

Setting CaseInsensitive has an impact on:

  • Letter Ranges. Any letter range (i.e '[A-Z]') will now match both lower or upper case characters.
  • Character Lists. Any character list (i.e '[ABC]') will now match both lower or upper case characters.
  • Literals. Any literal (i.e 'foo') will now match both lower or upper case characters i.e FoO will match foO etc.

Match Generation

Given a glob, you can generate random matches, or non matches, for that glob. For example, given the glob pattern /f?o/bar/**/*.txt you could generate matching strings like /foo/bar/ajawd/awdaw/adw-ad.txt or random non matching strings.

  var dotnetGlob = Glob.Parse(pattern);
  var generator = new GlobMatchStringGenerator(dotnetGlob.Tokens);

  for (int i = 0; i < 10; i++)
  {
          var testString = generator.GenerateRandomMatch();
          var result = dotnetGlob.IsMatch(testString);
          // result is always true.

          // generate a non match.
          testString = generator.GenerateRandomNonMatch();
          var result = dotnetGlob.IsMatch(testString);
           // result is always false.
  }

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.1 is compatible.  netstandard1.2 was computed.  netstandard1.3 was computed.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.0

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.6

    • No dependencies.
  • .NETStandard 1.1

NuGet packages (37)

Showing the top 5 NuGet packages that depend on DotNet.Glob:

Package Downloads
Dazinator.Extensions.FileProviders

Dazinator.Extensions.FileProviders

Casbin.NET

Casbin.NET is a powerful and efficient open-source access control library for .NET (C#) projects. It provides support for enforcing authorization based on various access control models.

Carbon.Feature.Web

Feature to support development and runtime of Web services.

OpenMod.Core The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Core service implementations for OpenMod

Traffk.StorageProviders.Common

Common set of functionality, base classes, and add-ons for Traffk.StorageProvider.Interfaces

GitHub repositories (21)

Showing the top 5 popular GitHub repositories that depend on DotNet.Glob:

Repository Stars
jellyfin/jellyfin
The Free Software Media System
Kareadita/Kavita
Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.
microsoft/kiota
OpenAPI based HTTP Client code generator
stryker-mutator/stryker-net
Mutation testing for .NET core and .NET framework!
belav/csharpier
CSharpier is an opinionated code formatter for c#.
Version Downloads Last updated
3.2.0-alpha.36 6,515 9/27/2021
3.2.0-alpha.33 233 3/5/2021
3.2.0-alpha.32 170 3/5/2021
3.2.0-alpha.31 176 3/5/2021
3.2.0-alpha.30 191 3/5/2021
3.2.0-alpha.29 277 12/26/2020
3.2.0-alpha.26 239 12/26/2020
3.2.0-alpha.25 238 12/26/2020
3.2.0-alpha.22 269 11/27/2020
3.2.0-alpha.21 1,155 8/21/2020
3.1.3 1,340,534 9/27/2021
3.1.2 382,797 12/26/2020
3.1.0 5,112,259 8/21/2020
3.1.0-issue-75.1 328 5/24/2020
3.1.0-alpha0009 69,573 8/3/2019
3.1.0-alpha0004 3,319 3/24/2019
3.1.0-alpha.51 268 8/21/2020
3.1.0-alpha.49 288 5/24/2020
3.1.0-alpha.47 285 3/5/2020
3.0.9 172,262 3/5/2020
3.0.9-beta.1 269 3/5/2020
3.0.8-beta0001 1,043 2/22/2020
3.0.5 239,813 4/2/2019
3.0.2-beta0001 1,312 4/2/2019
3.0.1 34,911 3/24/2019
3.0.1-beta0001 1,238 3/24/2019
3.0.0-alpha0052 1,265 3/24/2019
3.0.0-alpha0048 1,272 3/24/2019
3.0.0-alpha0045 1,359 2/11/2019
3.0.0-alpha0043 1,584 12/15/2018
3.0.0-alpha0041 2,675 9/12/2018
2.2.0-alpha0002 1,435 9/11/2018
2.2.0-alpha0001 1,459 8/24/2018
2.1.4 3,788 4/2/2019
2.1.3 1,718 3/24/2019
2.1.3-beta0001 1,360 3/24/2019
2.1.1 222,259 9/12/2018
2.1.0 3,724 8/24/2018
2.1.0-escaping0001 1,528 8/15/2018
2.1.0-alpha0034 1,468 8/24/2018
2.1.0-alpha0033 1,467 8/24/2018
2.1.0-alpha0022 1,595 8/10/2018
2.1.0-alpha0020 1,832 4/27/2018
2.0.3 117,385 4/27/2018
2.0.3-beta0001 2,012 4/27/2018
2.0.2 1,631 4/27/2018
2.0.1 100,809 3/27/2018
2.0.0 7,484 3/20/2018
2.0.0-alpha0139 1,417 3/11/2018
2.0.0-alpha0137 1,447 3/11/2018
2.0.0-alpha0130 1,421 3/11/2018
2.0.0-alpha0128 1,408 3/11/2018
2.0.0-alpha0127 1,440 3/11/2018
2.0.0-alpha0126 1,392 3/11/2018
2.0.0-alpha0125 1,386 3/11/2018
2.0.0-alpha0124 1,395 3/11/2018
2.0.0-alpha0123 1,403 3/11/2018
2.0.0-alpha0122 1,420 3/11/2018
2.0.0-alpha0121 1,378 3/11/2018
2.0.0-alpha0116 1,669 12/22/2017
2.0.0-alpha0115 1,409 12/22/2017
1.7.0-unstable0022 1,572 9/6/2017
1.7.0-unstable0018 1,334 8/27/2017
1.7.0-unstable0017 1,332 8/27/2017
1.7.0-unstable0013 1,334 7/12/2017
1.7.0-unstable0012 1,317 7/12/2017
1.7.0-unstable0009 1,416 6/30/2017
1.7.0-unstable0004 1,355 6/6/2017
1.7.0-unstable0001 1,376 5/18/2017
1.7.0-alpha0033 1,485 10/2/2017
1.6.9 9,681 10/2/2017
1.6.6 2,075 9/6/2017
1.6.5 1,732 8/27/2017
1.6.4 88,584 7/12/2017
1.6.3 1,676 6/30/2017
1.6.1 1,620 6/6/2017
1.6.0 3,100 5/18/2017
1.6.0-unstable0003 1,338 5/18/2017
1.6.0-unstable0002 1,347 5/12/2017
1.6.0-unstable0001 1,570 5/5/2017
1.5.0 1,551 5/5/2017
1.5.0-unstable0028 1,321 5/5/2017
1.5.0-unstable0027 1,379 5/5/2017
1.5.0-unstable0014 1,342 3/6/2017
1.5.0-unstable0013 1,311 3/6/2017
1.5.0-unstable0011 1,428 1/7/2017
1.5.0-unstable0010 1,387 1/5/2017
1.5.0-unstable0007 1,394 1/5/2017
1.5.0-unstable0006 1,424 12/19/2016
1.5.0-unstable0005 1,345 12/17/2016
1.5.0-unstable0001 1,398 12/15/2016
1.4.2 10,277 1/5/2017
1.4.1 9,738 12/17/2016
1.4.0 1,553 12/15/2016
1.4.0-unstable0004 1,405 12/15/2016
1.4.0-unstable0002 1,339 12/15/2016
1.4.0-unstable0001 1,412 12/14/2016
1.3.0 1,567 12/14/2016
1.3.0-unstable0021 1,345 12/12/2016
1.3.0-unstable0019 1,420 12/12/2016
1.3.0-unstable0018 1,408 12/12/2016
1.3.0-unstable0017 1,375 12/11/2016
1.3.0-unstable0016 1,407 12/11/2016
1.3.0-unstable0015 1,439 12/11/2016
1.3.0-unstable0014 1,447 12/10/2016
1.3.0-unstable0010 1,497 12/10/2016
1.3.0-unstable0008 1,440 12/10/2016
1.3.0-unstable0004 1,363 12/9/2016
1.3.0-unstable0002 1,415 12/9/2016
1.2.2 1,620 12/10/2016
1.2.1 1,605 12/9/2016
1.2.0-unstable0001 1,375 12/9/2016
1.1.0 1,700 12/9/2016
1.1.0-unstable0008 1,433 12/9/2016
1.1.0-unstable0005 1,416 12/7/2016
1.1.0-unstable0002 1,309 12/7/2016
1.1.0-unstable0001 1,348 12/7/2016
1.0.1 1,602 12/7/2016
1.0.0 1,595 12/7/2016
0.1.0 2,028 12/7/2016
0.1.0-unstable0012 1,420 12/7/2016

New features and fixes. See README in Github Repo.