Shimakaze.GeneratedRegex 0.0.1

dotnet add package Shimakaze.GeneratedRegex --version 0.0.1
                    
NuGet\Install-Package Shimakaze.GeneratedRegex -Version 0.0.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="Shimakaze.GeneratedRegex" Version="0.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shimakaze.GeneratedRegex" Version="0.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Shimakaze.GeneratedRegex">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 Shimakaze.GeneratedRegex --version 0.0.1
                    
#r "nuget: Shimakaze.GeneratedRegex, 0.0.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 Shimakaze.GeneratedRegex@0.0.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=Shimakaze.GeneratedRegex&version=0.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Shimakaze.GeneratedRegex&version=0.0.1
                    
Install as a Cake Tool

Shimakaze.GeneratedRegex

A lightweight, source-compatible polyfill for System.Text.RegularExpressions.GeneratedRegexAttribute on target frameworks older than .NET 7.

.NET 7 introduced [GeneratedRegex], which uses a source generator to implement partial Regex members. The attribute and the generator only ship with .NET 7 and later. This package brings the same source-level experience to .NET 6 and earlier (and .NET Framework) with a deliberately simple implementation:

partial Regex Foo() => new Regex(pattern, options);   // instead of the full compiled-engine codegen

How it works

When a compilation does not contain System.Text.RegularExpressions.GeneratedRegexAttribute (i.e. any framework before .NET 7), this generator:

  1. Injects a polyfill System.Text.RegularExpressions.GeneratedRegexAttribute so your code compiles unchanged, and
  2. Implements every marked partial method/property as an expression-bodied new Regex(pattern, options).

On .NET 7 and later the attribute is resolved from the runtime, the built-in source generator takes over, and this generator becomes a no-op (no conflicts).

Usage

Reference the generator from a project targeting a pre-.NET 7 framework (the example uses a ProjectReference, the NuGet package wires up the same way):

<ItemGroup>
  <ProjectReference Include="..\Shimakaze.GeneratedRegex\Shimakaze.GeneratedRegex.csproj"
                    OutputItemType="Analyzer"
                    ReferenceOutputAssembly="false" />
</ItemGroup>

Write exactly the same code you would on .NET 7+:

using System.Text.RegularExpressions;

public partial class SampleRegexes
{
    [GeneratedRegex(@"\d+", RegexOptions.IgnoreCase | RegexOptions.Multiline)]
    public partial Regex Numbers();

    [GeneratedRegex(@"[a-z]+", RegexOptions.Compiled, 1500)]
    public static partial Regex Words();

    [GeneratedRegex(@"^hello$")]
    public partial Regex Hello { get; }

    [GeneratedRegex(@"world", "invariant")]
    public partial Regex World { get; }
}

partial properties require C# 13 (e.g. <LangVersion>latest</LangVersion>). partial methods with accessibility require C# 9+.

What gets generated

For the example above the generator produces (per containing type):

namespace Sample
{
    public partial class SampleRegexes
    {
        public partial global::System.Text.RegularExpressions.Regex Numbers() =>
            new global::System.Text.RegularExpressions.Regex("\\d+",
                global::System.Text.RegularExpressions.RegexOptions.IgnoreCase
                | global::System.Text.RegularExpressions.RegexOptions.Multiline);

        public static partial global::System.Text.RegularExpressions.Regex Words() =>
            new global::System.Text.RegularExpressions.Regex("[a-z]+",
                global::System.Text.RegularExpressions.RegexOptions.Compiled,
                global::System.TimeSpan.FromMilliseconds(1500));

        public partial global::System.Text.RegularExpressions.Regex Hello =>
            new global::System.Text.RegularExpressions.Regex("^hello$",
                global::System.Text.RegularExpressions.RegexOptions.None);

        public partial global::System.Text.RegularExpressions.Regex World =>
            new global::System.Text.RegularExpressions.Regex("world",
                global::System.Text.RegularExpressions.RegexOptions.CultureInvariant);
    }
}

Every generated member creates a fresh new Regex(...) instance on each access — no caching.

Supported attribute arguments

All GeneratedRegexAttribute constructor overloads are supported:

  • pattern — the regex pattern
  • options — RegexOptions flags (any compile-time constant expression)
  • cultureName — "invariant" (or empty) maps to RegexOptions.CultureInvariant; other cultures produce warning GRG008 and are ignored (the runtime Regex uses the current culture)
  • matchTimeoutMilliseconds — emitted as TimeSpan.FromMilliseconds(...) in the constructor call

Both positional and named arguments are accepted.

Diagnostics

ID Severity Meaning
GRG001 Error The containing type must be partial
GRG002 Error The member must be partial
GRG003 Error Methods must not have parameters
GRG004 Error Members must not be generic
GRG005 Error The member must return System.Text.RegularExpressions.Regex
GRG006 Error Properties must be get-only
GRG007 Error Invalid/unsupported attribute arguments
GRG008 Warning Non-invariant culture is not supported and is ignored
GRG009 Error Unsupported target

License

MIT

There are no supported framework assets in this 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
0.0.1 159 8/8/2026