![]() |
VOOZH | about |
dotnet add package RegexMatcher --version 1.0.9
NuGet\Install-Package RegexMatcher -Version 1.0.9
<PackageReference Include="RegexMatcher" Version="1.0.9" />
<PackageVersion Include="RegexMatcher" Version="1.0.9" />Directory.Packages.props
<PackageReference Include="RegexMatcher" />Project file
paket add RegexMatcher --version 1.0.9
#r "nuget: RegexMatcher, 1.0.9"
#:package RegexMatcher@1.0.9
#addin nuget:?package=RegexMatcher&version=1.0.9Install as a Cake Addin
#tool nuget:?package=RegexMatcher&version=1.0.9Install as a Cake Tool
RegexMatcher is a library that maintains an internal dictionary of type <Regex, object>. Populate the dictionary with a series of Regex and the objects that should be returned when a match is found while evaluating some input.
For a sample app exercising the library please refer to the Test project.
Do you need help or have feedback? Contact me at joel at maraudersoftware.com dot com or file an issue here!
MatchPreference property to specify how to handle multiple match scenarios: first match, longest match, or shortest matchAllMatches API, thank you @sapurtcomputer30Always add Regex and return object in order from most specific to least specific. The first match found will be used and the associated object will be returned.
using RegexMatcher;
static void Main(string[] args)
{
Matcher matcher = new Matcher();
// preload a few
matcher.Add(new Regex("^/foo/\\d+$"), "foo with id");
matcher.Add(new Regex("^/foo/?$"), "foo with optional slash");
matcher.Add(new Regex("^/foo$"), "foo alone");
matcher.Add(new Regex("^/bar/(.*?)/(.*?)/?$"), "bar with two children");
matcher.Add(new Regex("^/bar/(.*?)/?$"), "bar with one child");
matcher.Add(new Regex("^/bar/\\d+$"), "bar with id");
matcher.Add(new Regex("^/bar/?$"), "bar with optional slash");
matcher.Add(new Regex("^/bar$"), "bar alone");
if (matcher.Match("/bar/child/foo", out object val1))
{
// val is "bar with two children"
}
if (matcher.Match("/foo/36", out object val2))
{
// val is "foo with id"
}
if (matcher.Match("/unknown", out object val3))
{
// won't get here
}
else
{
Console.WriteLine("Not found");
}
}
RegexMatcher uses standard C#/.NET regular expressions. I tested primarily against simple regular expressions with values that would be encountered as raw URLs/paths and it worked well.
Some notes that I found helpful which may help you too:
^ is a starting anchor, useful when indicating that the pattern must be matched at the start of the input$ is an ending anchor, useful when indicating that the pattern mst be matched at the end of the input(.*?) will match any input string\\d+ will match any number\\ the escape character must be used when matching certain characters as a literal? marks the previous character or expression as optionalThe library behavior when multiple matches are found can be configured using the Matcher.MatchPreference property.
First returns the first matchLongestFirst will return the longest matchShortestFirst will return the shortest matchHelpful links:
Refer to CHANGELOG.md for version history.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 was computed. net462 net462 is compatible. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 is compatible. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 3 NuGet packages that depend on RegexMatcher:
| Package | Downloads |
|---|---|
|
Watson
Simple, fast, async C# web server for handling REST requests with unified 7.x protocol architecture targeting .NET Standard 2.1, .NET 8, and .NET 10. |
|
|
Watson.Core
Core library for Watson 7.x shared semantics, routing, lifecycle, and protocol configuration targeting .NET 8 and .NET 10. |
|
|
HttpServerLite
User-space HTTP and HTTPS server without http.sys written in C#. Please visit the project site README for details on implementation. |
Showing the top 1 popular GitHub repositories that depend on RegexMatcher:
| Repository | Stars |
|---|---|
|
dotnet/WatsonWebserver
Watson is the fastest, easiest way to build scalable RESTful web servers and services in C#.
|
Retarget, all matches API, thank you @sapurtcomputer30