Language: EN

csharp-verbal-expressions

Build Regex Easily in C# with VerbalExpressions

CSharpVerbalExpressions is a .NET library that helps us build complex regular expressions (regex) more easily.

This library is the C# version of the popular JavaScript library VerbalExpressions. This project is dedicated to providing a simpler syntax style for using regular expressions.

VerbalExpressions uses a string-based approach to build regular expressions. Instead of writing a regular expression directly, we build a regular expression step by step using the methods of the library.

These methods have meaningful names like ‘Then’ or ‘Maybe’, which makes the code easier to read and understand. Thanks to VerbalExpressions, writing regex is much simpler.

Later, we can use VerbalExpressions on the dataset we want to work with, or generate the RegEx directly.

How to use CSharpVerbalExpressions

We can easily add the library to a .NET project, through the corresponding Nuget package.

Install-Package VerbalExpressions-official

Here are some examples of how to use CSharpVerbalExpressions extracted from the library’s documentation

var verbEx = new VerbalExpressions()
            .StartOfLine()
            .Then( "http" )
            .Maybe( "s" )
            .Then( "://" )
            .Maybe( "www." )
            .AnythingBut( " " )
            .EndOfLine();

// Create an example URL
var testMe = "https://www.google.com";

Assert.IsTrue(verbEx.Test( testMe ), "The URL is incorrect");

Console.WriteLine("We have a correct URL ");

CSharpVerbalExpressions is Open Source, and all the code and documentation is available in the project’s repository at https://github.com/VerbalExpressions/CSharpVerbalExpressions