用通配符匹配字符串

用通配符匹配字符串

问题描述:

我想用通配符(*)匹配字符串,其中通配符表示 any。例如:

I would like to match strings with a wildcard (*), where the wildcard means "any". For example:

*X = string must end with X
X* = string must start with X
*X* = string must contain X

此外,某些复合用法例如:

Also, some compound uses such as:

*X*YZ* = string contains X and contains YZ
X*YZ*P = string starts with X, contains YZ and ends with P.

有没有简单的算法可以做到这一点?我不确定使用正则表达式(尽管有可能)。

Is there a simple algorithm to do this? I'm unsure about using regex (though it is a possibility).

为澄清起见,用户将在上面的过滤器框中键入内容(就像一个简单的过滤器可能),我不希望他们自己编写正则表达式。因此,我可以轻松地从上述表示形式进行转换。

To clarify, the users will type in the above to a filter box (as simple a filter as possible), I don't want them to have to write regular expressions themselves. So something I can easily transform from the above notation would be good.

仅供参考,您可以 >使用VB.NET 类似操作员

Just FYI, you could use the VB.NET Like-Operator:

string text = "x is not the same as X and yz not the same as YZ";
bool contains = LikeOperator.LikeString(text,"*X*YZ*", Microsoft.VisualBasic.CompareMethod.Binary);  

如果需要,请使用 CompareMethod.Text

您需要使用Microsoft.VisualBasic.CompilerServices; 添加

You need to add using Microsoft.VisualBasic.CompilerServices;.