🎯

dotnet-source-gen-regex

🎯Skill

from im5tu/dotnet-skills

VibeIndex|
What it does

Converts .NET regex instances to compile-time source-generated implementations for improved performance and AOT compatibility.

πŸ“¦

Part of

im5tu/dotnet-skills(10 items)

dotnet-source-gen-regex

Installation

Quick InstallInstall with npx
npx skills add im5tu/dotnet-skills
πŸ“– Extracted from docs: im5tu/dotnet-skills
1Installs
-
AddedFeb 4, 2026

Skill Details

SKILL.md

Converts Regex instances to use the compile-time source generator. Use when the user wants to optimize regex performance or enable AOT compatibility.

Overview

# .NET Regex Source Generator

The regex source generator creates compile-time generated regex implementations that are:

  • AOT-compatible: Works with Native AOT and trimming
  • Debuggable: Step through the generated matching code
  • Performant: No runtime compilation overhead

When to Use

This skill applies when the user:

  • Wants to optimize regex performance
  • Needs AOT-compatible regex patterns
  • Asks about [GeneratedRegex] attribute
  • Mentions converting new Regex(...) to source-generated
  • Discusses regex compilation or startup performance

Workflow

  1. Find regex usages in the codebase:

- new Regex(...) constructor calls

- Regex.IsMatch(), Regex.Match(), Regex.Replace() static method calls

- Other static Regex.* methods with inline patterns

  1. For each regex with a compile-time known pattern:

- Ensure the containing class is partial

- Create a partial method with [GeneratedRegex] attribute:

```csharp

[GeneratedRegex("pattern", RegexOptions.IgnoreCase)]

private static partial Regex MyRegex();

```

- Name the method descriptively based on what the pattern matches

  1. Replace usages to call the generated method:

```csharp

// Before

var regex = new Regex(@"\d+", RegexOptions.Compiled);

if (regex.IsMatch(input)) { ... }

// After

if (MyNumberRegex().IsMatch(input)) { ... }

[GeneratedRegex(@"\d+")]

private static partial Regex MyNumberRegex();

```

  1. Verify with dotnet build
  1. If build fails, check:

- Class is marked partial

- Pattern is a compile-time constant

- .NET version is 7 or higher

Key Notes

| Note | Detail |

|------|--------|

| RegexOptions.Compiled | Ignored by source gen - remove it |

| .NET Version | Requires .NET 7+ |

| Caching | Generated method caches singleton internally |

| Timeout | Use [GeneratedRegex("pattern", RegexOptions.None, 1000)] for timeout (milliseconds) |

Pattern Conversion Examples

Instance with options:

```csharp

// Before

private readonly Regex _emailRegex = new(@"^[\w-\.]+@[\w-]+\.\w+$", RegexOptions.Compiled | RegexOptions.IgnoreCase);

// After

[GeneratedRegex(@"^[\w-\.]+@[\w-]+\.\w+$", RegexOptions.IgnoreCase)]

private static partial Regex EmailRegex();

```

Static method call:

```csharp

// Before

if (Regex.IsMatch(input, @"^\d{3}-\d{4}$"))

// After

if (PhoneNumberRegex().IsMatch(input))

[GeneratedRegex(@"^\d{3}-\d{4}$")]

private static partial Regex PhoneNumberRegex();

```

Error Handling

  • If pattern is not constant: Cannot use source gen - leave as runtime regex
  • If class is not partial: Add partial modifier to the class declaration
  • If build fails after conversion: Check error messages for unsupported pattern features

More from this repository9

🎯
dotnet-enable-testing-platform🎯Skill

Enables and configures testing platforms and frameworks for .NET projects, streamlining test setup and infrastructure.

🎯
dotnet-source-gen-logging🎯Skill

Transforms .NET logging calls to use LoggerMessage source generator for high-performance, AOT-compatible logging with zero boxing overhead.

🎯
dotnet-json-polymorphic🎯Skill

Converts JSON payloads with polymorphic type handling, enabling dynamic deserialization of complex .NET object hierarchies.

🎯
dotnet-source-gen-options-validation🎯Skill

Converts options validation to compile-time source generator for AOT-compatible, reflection-free validation at startup.

🎯
dotnet-source-gen-json🎯Skill

Generates JSON serialization and deserialization code for .NET classes using source generators, improving performance and reducing boilerplate.

🎯
dotnet-centralise-packages🎯Skill

Centralizes NuGet package versions across .NET projects by creating a single `Directory.Packages.props` file and updating project references.

🎯
dotnet-enable-autocomplete🎯Skill

Enables dotnet CLI tab autocomplete across multiple shells, automatically detecting .NET version and configuring shell-specific completion scripts.

🎯
dotnet-update-packages🎯Skill

Automatically lists and updates outdated NuGet packages in .NET projects, guiding users through dependency management with interactive confirmation.

🎯
dotnet-aot-analysis🎯Skill

Analyzes and configures .NET projects for Native AOT compatibility, applying source generators and optimizing AOT deployment settings.