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);
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
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With