C#字符串与通配符匹配

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

C#如何与通配符匹配字符串?通配符有两个标记,问题和星号。 ?表示任意字符,*表示任意字符。如何通过使用C#来实现此功能。谢谢。

C# how to achive string matching with wildcards?Wildcards there are two marks, question and asterisks. "?" Represents an arbitrary character, and "*" indicates that any arbitrary character.How to achive this function by use C#.Thank you.

推荐答案

请查看以下提示(不要把它当作文章,尽管如此分类):将通配符转换为正则表达式 [ ^ ] Look at the following tip (do not consider it an article, although so classified): Converting Wildcards to Regexes[^]

学习regexp: 30分钟正则表达式教程 [ ^ ] 现在使用w +等在*等正则表达式替换字符串 Learn regexp: The 30 Minute Regex Tutorial[^] Now do some string replacement on regex like * with w+ etc

尝试以下代码 try below code public static class WildcardMatch { #region Public Methods public static bool IsLike(string pattern, string text, bool caseSensitive=false) { pattern = pattern.Replace(".", @"\."); pattern = pattern.Replace("?", "."); pattern = pattern.Replace("*", ".*?"); pattern = pattern.Replace(@"\", @"\\"); pattern = pattern.Replace(" ", @"\s"); return new Regex(pattern, caseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase).IsMatch(text); } #endregion }

更多推荐

C#字符串与通配符匹配

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

发布评论

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

>www.elefans.com

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