π§© A fluent, intuitive, and thread-safe builder for regular expressions in C#
Rejigs makes creating complex regular expressions simple and readable by providing a fluent API that builds patterns step by step. Built with immutability at its core, Rejigs is inherently thread-safe and follows functional programming principles. No more cryptic regex syntax - write patterns that are easy to understand, maintain, and safely use across multiple threads!
- π§ Fluent API: Build complex regex patterns with readable, chainable methods
- π‘οΈ Thread-Safe: Immutable design ensures safe usage across multiple threads
- π Immutable: Each operation returns a new instance, preventing side effects
- β‘ Performance: No locking overhead thanks to immutable architecture
- π§ͺ Well-Tested: 369+ comprehensive tests ensuring reliability
Install Rejigs via NuGet Package Manager:
dotnet add package RejigsOr via Package Manager Console:
Install-Package Rejigs
using Rejigs;
// Simple text matching
var regex = Rejigs.Create()
.AtStart()
.Text("hello")
.AtEnd()
.Build();
Console.WriteLine(regex.IsMatch("hello")); // True
Console.WriteLine(regex.IsMatch("hello world")); // Falsevar emailRegex = Rejigs.Create()
.AtStart()
.OneOrMore(r => r.AnyLetterOrDigit().Or().AnyOf(".-_")) // Local part
.Text("@")
.OneOrMore(r => r.AnyLetterOrDigit().Or().AnyOf(".-")) // Domain
.Text(".")
.AnyInRange('a', 'z') // Top-level domain (2-6 letters)
.Between(2, 6)
.AtEnd()
.IgnoreCase() // Case-insensitive matching
.Build();
Console.WriteLine(emailRegex.IsMatch("[email protected]")); // True
Console.WriteLine(emailRegex.IsMatch("invalid-email")); // FalseRejigs is built with immutability at its core, making it inherently thread-safe without any performance overhead from locking mechanisms.
Every method call returns a new instance rather than modifying the existing one:
var basePattern = Rejigs.Create().Text("hello");
var pattern1 = basePattern.Text(" world"); // New instance
var pattern2 = basePattern.Text(" there"); // Another new instance
// basePattern remains unchanged: "hello"
// pattern1 contains: "hello world"
// pattern2 contains: "hello there"You can safely share Rejigs builders across multiple threads:
// Safe to use in multiple threads simultaneously
var sharedBuilder = Rejigs.Create()
.AtStart()
.OneOrMore(r => r.AnyDigit());
// Thread 1
var phonePattern = sharedBuilder.Text("-").Exactly(3, r => r.AnyDigit()).Build();
// Thread 2 (concurrent with Thread 1)
var idPattern = sharedBuilder.AtEnd().Build();
// No race conditions or shared state issues!- No locking overhead: Unlike mutable builders, no synchronization primitives needed
- Predictable performance: Operations are always O(1) for state creation
- Memory efficient: Only creates new instances when needed
- Garbage collector friendly: Short-lived intermediate objects
- Use descriptive variable names:
var emailRegex = ...instead ofvar regex = ... - Break complex patterns into smaller parts: Use variables to store intermediate builders
- Add comments: Explain what each part of your regex does
- Test thoroughly: Use unit tests to verify your patterns work correctly
- Use
AtStart()andAtEnd(): For exact matches, always anchor your patterns - Leverage immutability: Store base patterns and derive variations safely
- Thread-safe by design: Feel free to share builders across threads without locks
- Cache compiled regexes: Store the final
Build()result for repeated use - Functional composition: Chain operations naturally without side effects
// Create a base pattern (safe to share)
var baseValidation = Rejigs.Create().AtStart().OneOrMore(r => r.AnyLetterOrDigit());
// Multiple threads can safely extend the pattern
Task.Run(() => {
var emailPattern = baseValidation.Text("@").OneOrMore(r => r.AnyLetterOrDigit()).Build();
// Use emailPattern...
});
Task.Run(() => {
var usernamePattern = baseValidation.AtEnd().Build();
// Use usernamePattern...
});
// No synchronization needed!For detailed documentation, examples, and API reference, visit the Rejigs Documentation.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to raise an issue or a PR.