Skip to content

A simple .NET library for string comparison and matching using a fluent syntax.

License

Notifications You must be signed in to change notification settings

DotNetUtils/FluentMatch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FluentMatch

FluentMatch is a simple .NET library for string comparison and matching using a fluent syntax.

Getting Started

FluentMatch represents a string matching rule with the StringMatcher class. To create a StringMatcher, use one of the static methods available on the StringMatch class:

StringMatcher dogMatcher = StringMatch.Equals("dog");

The StringMatcher can then be used to test strings with the Matches method:

bool dogMatches = dogMatcher.Matches("dog");  // returns true
bool catMatches = dogMatcher.Matches("cat");  // returns false

StringMatcher can also operate on collections of strings:

string[] pets = new[] { "dog", "cat", "bird" };
bool hasDog = dogMatcher.MatchesAny(pets);    // returns true
bool onlyDogs = dogMatcher.MatchesAll(pets);  // returns false

FluentMatch includes a variety of string matching rules and options:

StringMatch.Contains("dog", StringComparison.Ordinal);
StringMatch.IsNotNullOrEmpty();
StringMatch.Regex(@"^[A-Z]{3} \d{3}$");

Complex Scenarios

FluentMatch provides options for combining multiple string matchers to create complex rules:

// Match any string containing either "dog" or "cat"
StringMatch.Contains("dog").Or(StringMatch.Contains("cat"));

// Match strings with specified start and end
StringMatch.StartsWith("a").And(StringMatch.EndsWith("z"));

StringMatcher can also be configured to apply a transformation to input strings before testing:

StringMatcher matcher = StringMatch.Equals("abc").WithTransform(n => n.Trim());
bool result = matcher.Matches("  abc ");  // returns true

Custom Matching Logic

If the built-in matching rules do not cover your use case, you can create a StringMatcher based on a delegate:

StringMatcher threeCharacters = StringMatch.Where(n => n.Length == 3);

You can also define matching logic by creating a custom StringMatcher class.

class ProductKeyMatcher : StringMatcher
{
    public override bool Matches(string input)
    {
        return input.StartsWith("PN-") && input.Skip(3).All(n => char.IsDigit(n));
    }
}

/* snip */

StringMatcher productKeyMatcher = new ProductKeyMatcher();
bool valid = productKeyMatcher.Matches("PN-1234");

About

A simple .NET library for string comparison and matching using a fluent syntax.

Topics

Resources

License

Stars

Watchers

Forks

Languages