Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A fast globbing library for .NET / .NETStandard applications. Outperforms Regex.

License

NotificationsYou must be signed in to change notification settings

dazinator/DotNet.Glob

Repository files navigation

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

say thanks - if you'd like this library enough please consider giving back with a small donation.

BranchBuild StatusNuGet
MasterBuild masterNuGet
DevelopBuild statusNuGet

This librarydoes not use Regex - I wanted to make something faster.The latest benchmarks show thatDotNet.Glob outperforms Regex - that was my goal for this library.The benchmarks useBenchmarkDotNet and can be located inside this repo. Justdotnet 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"); // You can also use ReadOnlySpan<char> on supported platforms.

Build a glob fluently

You can also use theGlobBuilder 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:

varglob=newGlobBuilder().PathSeparator().Literal("foo").AnyCharacter().PathSeparator(PathSeparatorKind.BackwardSlash).Wildcard().OneOf('a','b','c').NumberNotInRange('1','3').Literal(".txt").ToGlob();varisMatch=glob.IsMatch(@"/fooa\\barrra4.txt");// returns true.

Patterns

The following patterns are supported (from wikipedia):

WildcardDescriptionExampleMatchesDoes not match
*matches any number of any characters including noneLaw*Law, Laws, or Lawyer
?matches any single character?atCat, cat, Bat or batat
[abc]matches one character given in the bracket[CB]atCat or Batcat or bat
[a-z]matches one character from the range given in the bracketLetter[0-9]Letter0, Letter1, Letter2 up to Letter9Letters, Letter or Letter10
[!abc]matches one character that is not given in the bracket[!C]atBat, bat, or catCat
[!a-z]matches one character that is not from the range given in the bracketLetter[!3-5]Letter1, Letter2, Letter6 up to Letter9 and Letterx etc.Letter3, Letter4, Letter5 or Letterxx

In addition, DotNet Glob also supports:

WildcardDescriptionExampleMatchesDoes 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

Escaping special characters

Wrap special characters?, *, [ in square brackets in order to escape them.You can also use negation when doing this.

Here are some examples:

PatternDescriptionMatches
/foo/bar[[].bazmatch a[ after bar/foo/bar[.baz
/foo/bar[!!].bazmatch any character except! after bar/foo/bar7.baz
/foo/bar[!]].bazmatch any character except an ] after bar/foo/bar7.baz
/foo/bar[?].bazmatch an? after bar/foo/bar?.baz
/foo/bar[*]].bazmatch either a* or a] after bar/foo/bar*.baz,/foo/bar].baz
/foo/bar[*][]].bazmatch*] after bar/foo/bar*].baz

ReadOnlySpan

ReadOnlySpan<char> is supported as of version3.0.0 of this library.You can read more aboutSpan here:https://msdn.microsoft.com/en-us/magazine/mt814808.aspx

You must be targeting a platform that supportsReadOnlySpan<T> for this API to become available. These are currently:

  • .NET Core 2.1
  • Platforms that implement.NET Standard 2.1

Usage remains very similar, except you can use the overload that takes aReadOnlySpan<char> as opposed to astring:

    var glob = Globbing.Glob.Parse("p?th/*a[bcd]b[e-g]a[1-4][!wxyz][!a-c][!1-3].*");    var span = "pAth/fooooacbfa2vd4.txt".AsSpan();    Assert.True(glob.IsMatch(span));

There should be some performance benefits in utilising this in conjunction with otherSpan based API's being added to the .net framework / .net standard.

Advanced Usages

Options.

DotNet.Glob allows you to set options at a global level, however you can also override these options on a per glob basis, by passing in your ownGlobOptions instance to a glob.

To set global options, useGlobOptions.Default.

For example:

// Overide the default options globally for all matche:GlobOptions.Default.Evaluation.CaseInsensitive=true;DotNet.Globbing.Glob.Parse("foo").IsMatch("Foo");// true;

Or, override any global default options, by passing in your own instance ofGlobOptions:

GlobOptionsoptions=newGlobOptions();options.Evaluation.CaseInsensitive=false;DotNet.Globbing.Glob.Parse("foo",options).IsMatch("Foo");// false;

Case Sensitivity (Available as of version >= 2.0.0)

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

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

SettingCaseInsensitive 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.eFoO will matchfoO 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.  }

Give Back

If this library has helped you, even in a small way, please consider a small donation viahttps://opencollective.com/darrell-tunnellIt really would be greatly appreciated.

About

A fast globbing library for .NET / .NETStandard applications. Outperforms Regex.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    Contributors5

    Languages


    [8]ページ先頭

    ©2009-2025 Movatter.jp