简体   繁体   English

用于人类可读模式匹配的C#库?

[英]C# library for human readable pattern matching?

Does anybody know a C# library for matching human readable patterns? 有人知道C#库是否匹配人类可读模式? 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. 其中“this”和“that”是LIKE表达式,而“theother”由于引号而完全匹配。

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 因此,我正在使用与regex类似的方式,但使用人类可读的字符串,我的用户将很容易理解

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 这是一个额外的抽象层,但我个人更愿意阅读一个流畅的“中间阶段”,而不是自动生成的正则表达式:s

There is a good library called VerbalExpressions that basically constructs RegEx from a Fluent expression. 有一个很好的名为VerbalExpressions的库,它基本上是从Fluent表达式构建RegEx。 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");

Well, after a lot of searching, I wasn't able to find exactly what I was after, but needing to get something working pretty quickly, and due to the fact the system I'm using already has the relevant DLLs, I've ended up using Lucene.NET to created a temporary index containing a single document with the relevant fields I need to search added to it. 好吧,经过大量的搜索,我无法找到我到底的确切内容,但需要快速完成某些工作,并且由于我使用的系统已经具有相关的DLL,我已经最后使用Lucene.NET创建了一个包含单个文档的临时索引,其中包含我需要搜索的相关字段。 I can then do the type of query I'm after against it, and check for any matches. 然后我可以做我反对它的查询类型,并检查任何匹配。 By using the RAMDirectory class I was able to create the index in memory, and dispose of it after the lookup, so no index files have to be written to disk. 通过使用RAMDirectory类,我能够在内存中创建索引,并在查找后处理它,因此不必将索引文件写入磁盘。

I'm sure there are probably less intensive ways to achieve this, but as I say, it's the best I could come up with in the time I had. 我确信可能没有那么强化的方法来实现这一点,但正如我所说,这是我能想到的最好的方式。

Thank to everyone for their suggestions, and I would still like to know if there is a better way of doing this? 感谢大家的建议,我仍然想知道是否有更好的方法来做到这一点?

Several years ago I was looking for a way to define a more readable/intuitive syntax for the full-text search queries (SQL Server FTS). 几年前,我一直在寻找一种方法来为全文搜索查询(SQL Server FTS)定义更易读/直观的语法。 Then I found this article: Normalizing SQL Server Full-text Search Conditions . 然后我发现了这篇文章: 规范化SQL Server全文搜索条件

I hope it could be as helpful for you as it was for me. 我希望它对你有所帮助,对我来说也是如此。

What you want is for users to just type in their search criteria just like they would in Google. 您想要的是让用户只需输入他们的搜索条件,就像在Google中一样。 Some words, maybe some quoted phrases, maybe a few operators, and have it just work. 有些词,可能是一些引用的短语,可能是一些操作员,并且只是有效。 So, what to do? 那么该怎么办? Well, you could try and parse and rearrange the mixed bag of crap your users will submit into a valid normal form that CONTAINS and CONTAINSTABLE will accept. 好吧,您可以尝试解析并重新排列混合的垃圾袋,您的用户将提交为CONTAINS和CONTAINSTABLE将接受的有效正常形式。

What about this? 那这个呢?

Change this: 改变这个:

Regex socialSecurityNumberCheck = new Regex(@"^\d{3}-?\d{2}-?\d{4}$");

for this: 为了这:

Regex socialSecurityNumberCheck = new Regex(Pattern.With.AtBeginning
    .Digit.Repeat.Exactly(3)
    .Literal("-").Repeat.Optional
    .Digit.Repeat.Exactly(2)
    .Literal("-").Repeat.Optional
    .Digit.Repeat.Exactly(4)
    .AtEnd);

Download the binary here: http://flimflan.com/files/ReadableRex_DLL.zip 在这里下载二进制文件: http//flimflan.com/files/ReadableRex_DLL.zip

Visual Basic has a LIKE operator. Visual Basic有一个LIKE运算符。 It is much friendlier than RegEx. 它比RegEx更友好。 When I code in VB.NET, I can usually completely eliminate the need for RegEx by using LIKE. 当我在VB.NET中编码时,我通常可以通过使用LIKE完全消除对RegEx的需求。 In C#, you can either painfully deal with RegEx or create a VB library that uses the friendlier LIKE and reference it in your C# project. 在C#中,您可以痛苦地处理RegEx或创建一个使用更友好的LIKE并在C#项目中引用它的VB库。

Note: you will have to use RegEx for complex matching, but LIKE deals with most cases you run across. 注意:您必须使用RegEx进行复杂匹配,但LIKE会处理您遇到的大多数情况。

I think C# has already human readable pattern matcher - it's called LINQ. 我认为C#已经具有人类可读的模式匹配器 - 它被称为LINQ。

For example- LIKE operator can be emulated like this: 例如, LIKE运算符可以像这样模拟:

public static void Main (string[] args) {
  var found = "blood fold boot goat cook hole door".
               Split(' ').
               Where(part => part.Contains("oo"));

  foreach (var part in found)
    Console.WriteLine(part);
}

cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM