Fluent API for creating regular expression patterns.
$ dotnet add package Bromix.Fluent.RegularExpressionThe Bromix.Fluent.RegularExpression package provides a fluent API for building regular expression patterns in C#. This
can be useful in situations where regular expressions are complex or difficult to maintain, or when they
need to be generated dynamically based on external data inputs.
dotnet add package Bromix.Fluent.RegularExpression
Here are some simple examples to get a sense of how it works. For more detailed information, you can use the wiki.
The pattern could be \d{4} and can be written with Fluent Regular Expression like this.
using Bromix.Fluent.RegularExpression;
var pattern = Pattern
.With()
.Digit(Quantifier.Exactly(4))
.ToString();
var regex = new Regex(pattern);
var match = regex.Match("2023");
Console.WriteLine(match);
The pattern could be ^[a-zA-Z0-9._%\-+]+@[a-zA-Z0-9._]+.[a-zA-Z]{2,}$ and can be written with Fluent Regular
Expression like this.
using Bromix.Fluent.RegularExpression;
var pattern = Pattern
.With()
.StartOfLine()
.CharacterClass(Quantifier.OneOrMore, cc => cc
.Range('a', 'z')
.Range('A', 'Z')
.Range('0', '9')
.OneOf('.', '_', '%', '-', '+'))
.Literal("@")
.CharacterClass(Quantifier.OneOrMore, cc => cc
.Range('a', 'z')
.Range('A', 'Z')
.Range('0', '9')
.OneOf('.', '_'))
.Literal(".")
.CharacterClass(Quantifier.AtLeast(2), cc => cc
.Range('a', 'z')
.Range('A', 'Z'))
.EndOfLine()
.ToString();
var regex = new Regex(pattern);
var match = regex.Match(input);
Console.WriteLine(match.Success);