RegexBuilder 1.0.2

dotnet add package RegexBuilder --version 1.0.2
                    
NuGet\Install-Package RegexBuilder -Version 1.0.2
                    
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="RegexBuilder" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RegexBuilder" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="RegexBuilder" />
                    
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 RegexBuilder --version 1.0.2
                    
#r "nuget: RegexBuilder, 1.0.2"
                    
#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 RegexBuilder@1.0.2
                    
#: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=RegexBuilder&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=RegexBuilder&version=1.0.2
                    
Install as a Cake Tool

RegexBuilder

NuGet License: MIT

Just another day at the office, you write a .NET Regex like a boss, and suddenly realize that you need to declare, say, a non-capturing group. It's (?:pattern), right? Wait, or was it (?=pattern)? No no, (?=pattern) must be a positive lookahead or something. But if (?<=pattern) is a positive lookbehind, then maybe positive lookahead would be (?>=pattern)?

"Aaargh! Now where's that Regex cheat sheet?.." And make sure to share it with your five colleagues who might be maintaining this code later. Also, remember to use comments inside the Regex pattern, and maybe a few third-party tools to be sure what the expression does.

"How did it come to this?"

Inspired by the Expression Trees feature in .NET, the RegexBuilder library provides a more verbose but more human-readable way of declaring regular expressions, using a language friendly to the .NET world instead of two lines of cryptic mess.

When it might be useful:

  • When the expressions are complex and might be frequently changed.
  • When you can tolerate 20 lines of understandable code instead of 1 hardly understandable.
  • If you can spare a bit of CPU time and memory for constructing the Regex object for the sake of readability.

Compatibility

The library targets netstandard2.0, so it works with .NET Framework 4.6.1+, .NET Core 2.0+, and .NET 5+. Tests are verified against .NET 8 and .NET 10 LTS.

Example

Let's say you want to make a simple HTML parser and capture the value of every href attribute from hyperlinks, like shown in the MSDN example.

The usual way:

Regex hrefRegex = new Regex("href\\s*=\\s*(?:[\"'](?<Target>[^\"']*)[\"']|(?<Target>\\S+))", RegexOptions.IgnoreCase);

With RegexBuilder:

const string quotationMark = "\"";
Regex hrefRegex = RegexBuilder.Build
(
    RegexOptions.IgnoreCase,
    // Regex structure declaration
    RegexBuilder.Literal("href"),
    RegexBuilder.MetaCharacter(RegexMetaChars.WhiteSpace, RegexQuantifier.ZeroOrMore),
    RegexBuilder.Literal("="),
    RegexBuilder.MetaCharacter(RegexMetaChars.WhiteSpace, RegexQuantifier.ZeroOrMore),
    RegexBuilder.Alternate
    (
        RegexBuilder.Concatenate
        (
            RegexBuilder.NonEscapedLiteral(quotationMark),
            RegexBuilder.Group
            (
                "Target",
                RegexBuilder.NegativeCharacterSet(quotationMark, RegexQuantifier.ZeroOrMore)
            ),
            RegexBuilder.NonEscapedLiteral(quotationMark)
        ),
        RegexBuilder.Group
        (
            "Target",
            RegexBuilder.MetaCharacter(RegexMetaChars.NonwhiteSpace, RegexQuantifier.OneOrMore)
        )
    )
);

See CustomRegexTests.cs for more examples.

Feature Support

RegexBuilder currently supports all regular expression language elements except substitution/replacement patterns.

The following elements are supported:

Installation

Install via the .NET CLI:

dotnet add package RegexBuilder

Or use the NuGet Package Manager Console:

PM> Install-Package RegexBuilder

Usage Guide

There are 3 classes you'll need. They all expose their functionality via static members and work statelessly.

  1. RegexBuilder: a factory class that produces and glues together different parts of a regular expression.
  2. RegexQuantifier: produces quantifiers (?, + {4,}, etc.) for regex parts that support them.
  3. RegexMetaChars: named constants for character classes (word boundary, whitespace, tab, etc.).

Start with var regex = RegexBuilder.Build(...); and replace ... with the parts of your regular expression by calling the corresponding methods of RegexBuilder.

Development

Build the solution:

dotnet build src/YuriyGuts.RegexBuilder.sln

Run the tests (runs on both net8.0 and net10.0):

dotnet test --project src/YuriyGuts.RegexBuilder.Tests

Run the console test app:

dotnet run --project src/YuriyGuts.RegexBuilder.TestApp

The project has GeneratePackageOnBuild=true, so a .nupkg file is produced on every build.

License

The source code is licensed under The MIT License.

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.  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 was computed.  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 was computed. 
.NET Framework 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 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.
  • .NETStandard 2.0

    • 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.

Version Downloads Last Updated
1.0.2 1,484 2/19/2026
1.0.1 119 2/19/2026
1.0.0 31,345 1/6/2014

Update test/demo projects to .NET 8/10 LTS; migrate tests to MSTest SDK 4.1.0