AgentStack
SKILL verified MIT Self-run

Dotnet Source Gen Regex

skill-im5tu-claude-dotnet-source-gen-regex · by Im5tu

Converts Regex instances to use the compile-time source generator. Also use when the user mentions "GeneratedRegex," "regex source generator," "compile-time regex," "AOT regex," "optimize regex," or "source-generated regex." For full AOT analysis, see dotnet-aot-analysis.

No reviews yet
0 installs
9 views
0.0% view→install

Install

$ agentstack add skill-im5tu-claude-dotnet-source-gen-regex

✓ scanned · ✓ verified — works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • Prompt-injection patterns
  • Secret / credential exfiltration
  • Dangerous shell & filesystem operations
  • Untrusted network calls
  • Known-malicious package signatures

What it can access

  • Network access No
  • Filesystem access No
  • Shell / process execution No
  • Environment & secrets No
  • Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

Are you the author of Dotnet Source Gen Regex? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

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

// 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:

// 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

Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

Install and usage instructions live in the source repository linked above.

Reviews

No reviews yet — be the first.

Versions

  • v0.1.0 Imported from the upstream source.