Need to perform Wildcard (*,?, etc) search on a string using Regex

后端 未结 10 2453
执笔经年
执笔经年 2020-11-27 04:35

I need to perform Wildcard (*, ?, etc.) search on a string. This is what I have done:

string input = \"Message\";
string pattern =          


        
10条回答
  •  猫巷女王i
    2020-11-27 05:14

    From http://www.codeproject.com/KB/recipes/wildcardtoregex.aspx:

    public static string WildcardToRegex(string pattern)
    {
        return "^" + Regex.Escape(pattern)
                          .Replace(@"\*", ".*")
                          .Replace(@"\?", ".")
                   + "$";
    }
    

    So something like foo*.xls? will get transformed to ^foo.*\.xls.$.

提交回复
热议问题