Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# library for human readable pattern matching?

Does anybody know a C# library for matching human readable patterns? Similar to regex, but friendlier?

Given a string value, I want to be able to match it against a pattern along the lines of:

(this AND that) OR "theother"

where "this" and "that" are LIKE expressions, and "theother" is an exact match due to the quotes.

UPDATE: Ok, just to be a little bit clearer. The reason I want this is to allow end users to enter in their own patterns, as string values. So I'm after something that works in a similar way to regex, but uses human readable strings that my users will easily understand

var pattern = "(this AND that) OR \"theother\""; // Could be fetched from textbox
var match = SomeLib.IsMatch(myString, pattern);
like image 242
Matt Brailsford Avatar asked Mar 25 '11 13:03

Matt Brailsford


2 Answers

I read this article a while back. It sounds along the lines of what you are asking.

Readable Regular Expressions

Which, looking at your request, you would then need to create a mapping of 'user friendly' terminology and this library's fluent interface.

It's an extra layer of abstraction, true but I personally, would rather read a fluent 'intermediate stage' than auto generated regex :s

like image 178
MattC Avatar answered Sep 24 '22 14:09

MattC


There is a good library called VerbalExpressions that basically constructs RegEx from a Fluent expression. Here is an example:

// Create an example of how to test for correctly formed URLs
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");
like image 36
Icemanind Avatar answered Sep 23 '22 14:09

Icemanind