Tedd.WildcardMatch
1.0.2
dotnet add package Tedd.WildcardMatch --version 1.0.2
NuGet\Install-Package Tedd.WildcardMatch -Version 1.0.2
<PackageReference Include="Tedd.WildcardMatch" Version="1.0.2" />
paket add Tedd.WildcardMatch --version 1.0.2
#r "nuget: Tedd.WildcardMatch, 1.0.2"
// Install Tedd.WildcardMatch as a Cake Addin #addin nuget:?package=Tedd.WildcardMatch&version=1.0.2 // Install Tedd.WildcardMatch as a Cake Tool #tool nuget:?package=Tedd.WildcardMatch&version=1.0.2
Tedd.WildcardMatch
Fast and reliable .Net library for wildcard (* and ?) matching capable of complex pattern matching.
Available as NuGet package: https://www.nuget.org/packages/Tedd.WildcardMatch
Fast
One match takes 0.0000003278 on a modern computer. There are faster libraries, but they are not reliable.
Reliable
Many (of not most) examples of wildcard matching found on the web fail to implement proper support for wildcard patterns, meaning they will not always give the intended result. This library uses the Regex engine in .Net to implement proper wildcard support. This means it capable of reliably matching complex wildcard patterns. (See further down for example of how the "FastWildcard"-library advertising "no edge-cases" in the NuGet listing breaks down on a simple match.)
Example
Extension method
// Standard matching (case sensitive)
var match = "Lorem ipsum".IsWildcardMatch("or*ips?m");
// Will not match because L in Lorem is incorrect case
var caseNotMatch = "Lorem ipsum".IsWildcardMatch("lor?m");
// Set it to ignore case
var caseMatch = "Lorem ipsum".IsWildcardMatch("lor?m", true);
Static
// Standard matching (case sensitive)
var alsoMatch = WildcardMatch.IsMatch("Lorem", "L??em");
// Use Options to set it to ignore case
var andThis = WildcardMatch.IsMatch("lorem", "L?REM", WildcardOptions.IgnoreCase);
Instance
var wm = new WildcardMatch("or*ips?m");
// Standard match
var match1 = wm.IsMatch("Lorem ipsum");
// Reuse object for faster second match
var match2 = wm.IsMatch("Bored Chipsom");
// A compiled instance is slighly slower at startup
var wmc = new WildcardMatch("or*ips?m", WildcardOptions.Compiled | WildcardOptons.IgnoreCase);
// But matching is faster
var match3 = wmc.IsMatch("More ipsums");
WildcardOptions
Option | Description |
---|---|
None | Specifies that no options are set. |
IgnoreCase | Specifies case-insensitive matching. |
Singleline | Specifies single-line mode. Changes the meaning of the star (*) and questionmark (?) so they match every character (instead of every character except \n). |
Compiled | Specifies that the regular expression is compiled to an assembly. This yields faster execution but increases startup time. |
CultureInvariant | Specifies that cultural differences in language is ignored. |
RightToLeft | Specifies that the search will be from right to left instead of from left to right. |
Tips on performance
User input
Remember that overuse of multiple wildcards (especially star) on large amounts of text may lead to high CPU usage. By default the matcher will run infinitely. If you want to limit the time it runs to for example 0.1 seconds then you must provide a timeout parameter when creating class instance.
In case where you take wildcard from user it is advicable to implement a limit so that user can't do Denial Of Service by crafting special wildcard patterns.
Slow (0.000004 seconds to match)
Using extension method or static methods invokes parsed execution, this is relatively slow by all means. "Slow" in this context means less than 0.000001 seconds, so unless you are planning to parse a lot of matches you won't notice it.
Faster: Reusing instance
If you intend to reuse same pattern om multiple matches then it may beneficial to use an instanced WildcardMatch. By instancing the pattern match you save the setup-time.
Fastest: Precompiled
Providing the WildcardOptions.Compiled option will cause slightly higher start cost as the match is compiled into the assembly, but give better performance on matches. One of the major benefints here is that once object is set up it can perform matching with zero memory allocations, which is good for GC.<br /> This approach scales very well, giving very high performance on complext pattern matches and will not lead to GC hickups.
Product | Versions 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 is compatible. netcoreapp2.1 is compatible. netcoreapp2.2 is compatible. netcoreapp3.0 is compatible. netcoreapp3.1 is compatible. |
.NET Standard | netstandard1.2 is compatible. netstandard1.3 is compatible. netstandard1.4 is compatible. netstandard1.5 is compatible. netstandard1.6 is compatible. netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net451 was computed. net452 was computed. net46 is compatible. net461 is compatible. net462 is compatible. net463 was computed. net47 is compatible. net471 is compatible. net472 is compatible. net48 is compatible. 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 | netcore451 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 2.0
- No dependencies.
-
.NETCoreApp 2.1
- No dependencies.
-
.NETCoreApp 2.2
- No dependencies.
-
.NETCoreApp 3.0
- No dependencies.
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 4.6
- No dependencies.
-
.NETFramework 4.6.1
- No dependencies.
-
.NETFramework 4.6.2
- No dependencies.
-
.NETFramework 4.7
- No dependencies.
-
.NETFramework 4.7.1
- No dependencies.
-
.NETFramework 4.7.2
- No dependencies.
-
.NETFramework 4.8
- No dependencies.
-
.NETStandard 1.2
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 1.3
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 1.4
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 1.5
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 1.6
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Method documentation.
Exposes Wildcard and WildcardRegex for instanced match.