用通配符匹配字符串

编程入门 行业动态 更新时间:2024-10-25 17:26:49
本文介绍了用通配符匹配字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

用通配符匹配字符串

本文发布于:2023-10-24 14:54:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!