用通配符匹配字符串 [英] Matching strings with wildcard

查看:233
本文介绍了用通配符匹配字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用通配符(*)匹配字符串,其中通配符表示 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;.

这篇关于用通配符匹配字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆