using-generated-regex
SolidImplements compile-time regex using GeneratedRegexAttribute Source Generator. Use when working with Regex patterns for better performance and AOT compatibility.
Code & Development 40 stars
6 forks Updated 6 days ago MIT
Install
Quality Score: 78/100
Stars 20%
Recency 20%
Frontmatter 20%
Documentation 15%
Issue Health 10%
License 10%
Description 5%
Skill Content
# Using GeneratedRegex (Source Generator)
Use `GeneratedRegexAttribute` for compile-time regex generation instead of runtime `new Regex()`.
## Why GeneratedRegex?
| Aspect | Runtime Regex | GeneratedRegex |
|--------|--------------|----------------|
| Compilation | Runtime | Compile-time |
| Performance | Slower first match | Pre-compiled, faster |
| AOT Support | Limited | Full support |
| Memory | Allocates at runtime | No runtime allocation |
| .NET Version | All | .NET 7+ |
---
## Basic Pattern
```csharp
public partial class EmailValidator
{
[GeneratedRegex(@"^[\w\.-]+@[\w\.-]+\.\w+$", RegexOptions.IgnoreCase)]
private static partial Regex EmailPattern();
public bool IsValidEmail(string email)
{
return EmailPattern().IsMatch(email);
}
}
```
**Requirements:**
- Class must be `partial`
- Method must be `static partial` returning `Regex`
- .NET 7 or later
---
## Common Patterns
### Email Validation
```csharp
public partial class ValidationPatterns
{
[GeneratedRegex(@"^[\w\.-]+@[\w\.-]+\.\w+$", RegexOptions.IgnoreCase)]
public static partial Regex Email();
}
```
### Phone Number
```csharp
[GeneratedRegex(@"^\d{3}-\d{3,4}-\d{4}$")]
public static partial Regex PhoneNumber();
```
### URL Pattern
```csharp
[GeneratedRegex(@"^https?://[\w\.-]+(?:/[\w\.-]*)*$", RegexOptions.IgnoreCase)]
public static partial Regex Url();
```
### Whitespace Normalization
```csharp
[GeneratedRegex(@"\s+")]
private static partial Regex WhitespacePa...
Details
- Author
- christian289
- Repository
- christian289/dotnet-with-claudecode
- Created
- 7 months ago
- Last Updated
- 6 days ago
- Language
- C#
- License
- MIT
Similar Skills
Semantically similar based on skill content — not just same category
AI & Automation Featured
csharp-rules
C#/.NET coding rules: style, patterns, security, testing. Triggers: .cs, .csproj, .sln, ASP.NET, ASP.NET Core, EF Core, LINQ, NUnit, xUnit, dotnet.
161 Updated today
softspark AI & Automation Featured
code-generation
Minimal, pattern-matching code output. Write the least code that satisfies requirements. Match existing project patterns. Use Write/Edit tools only.
1,591 Updated yesterday
a5c-ai AI & Automation Listed
dotnet-skills
.NET Core/ASP.NET patterns, best practices, and implementation guides
0 Updated today
murtazatouqeer