Use the [GeneratedRegex] source generator for ALL regex in .NET 7+ C#. Produces AOT-safe, trimming-friendly, compile-time regex with zero startup cost and better throughput than RegexOptions.Compiled. Trigger whenever the user writes, reviews, or asks about regex in C# — including new Regex(...), Regex.IsMatch, pattern matching, string validation, or text parsing — even if they don't mention source generation. Never suggest new Regex(...) with RegexOptions.Compiled when [GeneratedRegex] is available.
Installation
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Use the [GeneratedRegex] source generator for ALL regex in .NET 7+ C#. Produces AOT-safe, trimming-friendly, compile-time regex with zero startup cost and better throughput than RegexOptions.Compiled. Trigger whenever the user writes, reviews, or asks about regex in C# — including new Regex(...), Regex.IsMatch, pattern matching, string validation, or text parsing — even if they don't mention source generation. Never suggest new Regex(...) with RegexOptions.Compiled when [GeneratedRegex] is available.
C# Source-Generated Regex Skill
Use [GeneratedRegex] on a partial method or property so the compiler emits a fully-optimised, AOT-safe Regex implementation at build time. This delivers the throughput of RegexOptions.Compiled with near-zero startup cost, and the output is ordinary C# that you can read and debug.
Why this matters
new Regex(pattern, RegexOptions.Compiled) pays a heavy startup cost (reflection-emit, JIT compilation) and is not AOT-safe. [GeneratedRegex] compiles the pattern into readable C# during the build, giving you:
Better throughput than RegexOptions.Compiled (and more, in some benchmarks)
Near-zero startup cost — no JIT compile at runtime
AOT / trimming safe — no Reflection.Emit
Debuggable — step through the generated C# in any debugger
Automatic singleton caching — no static readonly field needed
Diagnostic SYSLIB1045 fires when the compiler detects a place that could use [GeneratedRegex] but doesn't.