RegexMatcher 1.1.1

dotnet add package RegexMatcher --version 1.1.1
                    
NuGet\Install-Package RegexMatcher -Version 1.1.1
                    
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="RegexMatcher" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RegexMatcher" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="RegexMatcher" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RegexMatcher --version 1.1.1
                    
#r "nuget: RegexMatcher, 1.1.1"
                    
#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.
#:package RegexMatcher@1.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RegexMatcher&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=RegexMatcher&version=1.1.1
                    
Install as a Cake Tool

RegexMatcher

NuGet Version NuGet

Regex Matching Library in C#

RegexMatcher is a library that maintains an ordered list of Regex and object pairs. Populate it with a series of Regex and the objects that should be returned when a match is found while evaluating some input. All methods are thread-safe.

For examples exercising every API, refer to the test suites in src/Test.Shared.

Help or Feedback

Do you need help or have feedback? Contact me at joel at maraudersoftware.com dot com or file an issue here!

New in v1.1.1

  • Test dependency updates (Touchstone 0.2.0, NUnit 5.0.0, Microsoft.NET.Test.Sdk 18.10.1, and others); no changes to the library or its public API

New in v1.1.0

  • ValueExists compares values with Object.Equals, so equal strings and boxed value types are found
  • Get returns a snapshot copy that is safe to enumerate while other threads modify the matcher
  • Entries are always evaluated in the order they were added, including after removals
  • Retargeted to .NET 8.0 and .NET 10.0 (plus .NET Standard 2.0/2.1 and .NET Framework 4.6.2/4.8)

Refer to CHANGELOG.md for upgrade notes.

Important Notes

Always add Regex and return object in order from most specific to least specific. Entries are evaluated in the order they were added; with the default MatchPreference of First, the first match found is used and the associated object is returned.

Regular expressions are identified by instance. Two distinct Regex instances with the same pattern are separate entries, and Exists and Remove require the same instance that was passed to Add. Adding the same instance twice throws ArgumentException.

Match and AllMatches throw ArgumentNullException for null or empty input. A RegexMatchTimeoutException from a Regex constructed with a timeout is passed through to the caller.

Simple Example

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");
    }
}

Regular Expression Notes

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 must 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 optional

Multiple Matches

The library behavior when multiple matches are found can be configured using the Matcher.MatchPreference property.

  • First (default) returns the value for the first matching regex, in insertion order
  • LongestFirst returns the value for the matching regex with the longest pattern string, which usually indicates the most specific pattern
  • ShortestFirst returns the value for the matching regex with the shortest pattern string

LongestFirst and ShortestFirst compare the length of the regex pattern text, not the length of the matched input. Ties go to the entry added first. AllMatches ignores MatchPreference and returns every matching value in insertion order.

Helpful links:

Running Tests

Tests are written once as Touchstone descriptors in src/Test.Shared and run through any of three runners:

# Console runner (exit code 0 on success, 1 on failure)
dotnet run --project src/Test.Automated -f net8.0

# Export results to JSON
dotnet run --project src/Test.Automated -f net8.0 -- --results results.json

# xUnit and NUnit runners
dotnet test src/Test.Xunit
dotnet test src/Test.Nunit

Version History

Refer to CHANGELOG.md for version history.

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 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 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.6.2

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (3)

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.

GitHub repositories (1)

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#.
Version Downloads Last Updated
1.1.1 0 10/3/2026
1.1.0 31 10/3/2026
1.0.9 78,935 1/7/2025
1.0.8 98,597 4/24/2023
1.0.7.2 137,157 2/7/2022
1.0.7.1 24,897 11/12/2021
1.0.7 93,615 11/15/2020
1.0.5 5,640 10/14/2020
1.0.3 34,663 9/21/2019
1.0.2 44,087 6/13/2019
1.0.1 9,927 3/10/2019
1.0.0 13,317 4/27/2017

Test dependency updates (Touchstone 0.2.0, NUnit 5.0.0, Microsoft.NET.Test.Sdk 18.10.1); no library or API changes