Shimakaze.GeneratedRegex
0.0.1
dotnet add package Shimakaze.GeneratedRegex --version 0.0.1
NuGet\Install-Package Shimakaze.GeneratedRegex -Version 0.0.1
<PackageReference Include="Shimakaze.GeneratedRegex" Version="0.0.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Shimakaze.GeneratedRegex" Version="0.0.1" />
<PackageReference Include="Shimakaze.GeneratedRegex"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Shimakaze.GeneratedRegex --version 0.0.1
#r "nuget: Shimakaze.GeneratedRegex, 0.0.1"
#:package Shimakaze.GeneratedRegex@0.0.1
#addin nuget:?package=Shimakaze.GeneratedRegex&version=0.0.1
#tool nuget:?package=Shimakaze.GeneratedRegex&version=0.0.1
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:
- Injects a polyfill
System.Text.RegularExpressions.GeneratedRegexAttributeso your code compiles unchanged, and - Implements every marked
partialmethod/property as an expression-bodiednew 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; }
}
partialproperties require C# 13 (e.g.<LangVersion>latest</LangVersion>).partialmethods 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 patternoptions—RegexOptionsflags (any compile-time constant expression)cultureName—"invariant"(or empty) maps toRegexOptions.CultureInvariant; other cultures produce warningGRG008and are ignored (the runtimeRegexuses the current culture)matchTimeoutMilliseconds— emitted asTimeSpan.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
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 |