Agent skill
dotnet-source-gen-regex
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.
Install this agent skill to your Project
npx add-skill https://github.com/Im5tu/claude/tree/main/skills/dotnet-source-gen-regex
Metadata
Additional technical details for this skill
- author
- Im5tu
- version
- 1.0
- repositoryUrl
- https://github.com/im5tu/dotnet-skills
SKILL.md
.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
-
Find regex usages in the codebase:
new Regex(...)constructor callsRegex.IsMatch(),Regex.Match(),Regex.Replace()static method calls- Other static
Regex.*methods with inline patterns
-
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
- Ensure the containing class is
-
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(); -
Verify with
dotnet build -
If build fails, check:
- Class is marked
partial - Pattern is a compile-time constant
- .NET version is 7 or higher
- Class is marked
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
partialmodifier to the class declaration - If build fails after conversion: Check error messages for unsupported pattern features
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
flutter-animations
Comprehensive guide for implementing animations in Flutter. Use when adding motion and visual effects to Flutter apps: implicit animations (AnimatedContainer, AnimatedOpacity, TweenAnimationBuilder), explicit animations (AnimationController, Tween, AnimatedWidget/AnimatedBuilder), hero animations (shared element transitions), staggered animations (sequential/overlapping), and physics-based animations. Includes workflow for choosing the right animation type, implementation patterns, and best practices for performance and user experience.
dotnet-source-gen-logging
Converts logging to use the LoggerMessage source generator for high-performance, AOT-compatible logging. Also use when the user mentions "LoggerMessage," "logging source generator," "high-performance logging," "optimize logging," "AOT logging," or "structured logging source gen." For full AOT analysis, see dotnet-aot-analysis.
dotnet-source-gen-options-validation
Converts options validation to use the compile-time source generator for AOT-compatible, reflection-free validation. Also use when the user mentions "OptionsValidator," "options validation source gen," "AOT options validation," "compile-time validation," "ValidateDataAnnotations replacement," or "reflection-free validation." For full AOT analysis, see dotnet-aot-analysis.
dotnet-enable-testing-platform
Enables the Microsoft Testing Platform runner in global.json. Also use when the user mentions "testing platform," "Microsoft.Testing.Platform," "new test runner," "migrate test runner," "global.json testing," or "modern test platform."
dotnet-json-polymorphic
Configures polymorphic JSON serialization with [JsonPolymorphic] and [JsonDerivedType] attributes. Also use when the user mentions "polymorphic JSON," "JsonDerivedType," "JSON inheritance," "type discriminator," "serialize derived types," or "JSON polymorphism." For full JSON source generation, see dotnet-source-gen-json.
competitor-alternatives
When the user wants to create competitor comparison or alternative pages for SEO and sales enablement. Also use when the user mentions 'alternative page,' 'vs page,' 'competitor comparison,' 'comparison page,' '[Product] vs [Product],' '[Product] alternative,' or 'competitive landing pages.' Covers four formats: singular alternative, plural alternatives, you vs competitor, and competitor vs competitor. Emphasizes deep research, modular content architecture, and varied section types beyond feature tables.
Didn't find tool you were looking for?