RegexBuilder.NET9 1.0.2

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

RegexBuilder.NET9

Note: This is a fork of the original regex-builder by Yuriy Guts, updated to support .NET 9.

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.

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:

How to Integrate RegexBuilder

Add a reference to RegexBuilder.dll in your project manually, or use NuGet Package Manager:

PM> Install-Package RegexBuilder.NET9

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.

Testing

RegexBuilder uses MSTest for unit testing. To run or add unit tests in Visual Studio, please see the RegexBuilder.Tests project.

The RegexBuilder.TestApp project is a console application that can be used as a temporary testing workbench.

License

The source code is licensed under The MIT License.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.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.1.2 223 10/22/2025
1.1.1 215 10/21/2025
1.1.1-a 212 10/22/2025
1.1.0 215 10/20/2025
1.0.5 218 10/20/2025
1.0.4 224 10/20/2025
1.0.3 209 10/20/2025
1.0.2 217 10/20/2025