FluentRegex is a .NET library for building regular expressions in a readable and understandable way. It adopts the Builder Pattern. This library is intended to make it easier to write and understand regular expressions, especially for those who are not familiar with them.
Build Status | Test Status (main) |
---|---|
I originally started working on this alone, and after getting through the first couple of iterations, I found some similar projects that seemed to be accomplishing a lot of what I had set out to do. This compelled me to fork from Anders Åberg's project, and eventually re-write the library completely. Given their impact on my work, I felt it would only be right to share links to their projects here:
Here are some examples of how you can use FluentRegex:
Note: This is not the most efficient way to validate an email address. It is only an example of how to use FluentRegex.
class Main
{
public PatternBuilder builder = new PatternBuilder();
public string output = "";
Main()
{
output = builder.StartAnchor()
.StartOfLine()
.Build()
.StartGroup()
.StartGroup()
.StartCharacterClass()
.Word()
.Build()
.Or()
.StartCharacterClass()
.StartCustomPattern()
.AppendLiteral("_")
.AppendLiteral("-")
.AppendLiteral(".")
.AppendLiteral("+")
.Build()
.Build()
.Build()
.Times(1, -1)
.AppendLiteral("@")
.Times(1, 1)
.StartGroup()
.StartCharacterClass()
.Word()
.Build()
.Or()
.StartCharacterClass()
.StartCustomPattern()
.AppendLiteral("_")
.AppendLiteral("-")
.AppendLiteral(".")
.Build()
.Build()
.Build()
.Times(1, -1)
.StartAnchor()
.EndOfLine()
.Build()
.Build()
.ToString();
Console.WriteLine(output);
}
}
// Output: ^((\w|[_\-.+])+@{1}(\w|[_\-.])+$)
Match either "github" or "bitbucket" (^github|bitbucket$
):
class Main
{
public PatternBuilder builder = new PatternBuilder();
public string output = "";
Main()
{
output = builder.StartAnchor()
.StartOfLine()
.Build()
.StartGroup()
.AppendLiteral("github")
.Or()
.AppendLiteral("bitbucket")
.Build()
.StartAnchor()
.EndOfLine()
.Build()
.ToString();
Console.WriteLine(output);
}
}
For more examples, refer to the tests in the FluentRegex.Tests
project.
Additional documentation is available in the docs
folder. See the Index file for more information.
Any feedback and contributions are appreciated. If you're not great at writing regex, your input can be especially valuable to us!