Quantcast
Viewing all articles
Browse latest Browse all 12

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

You need to convert your wildcard expression to a regular expression. For example:

    private bool WildcardMatch(String s, String wildcard, bool case_sensitive)    {        // Replace the * with an .* and the ? with a dot. Put ^ at the        // beginning and a $ at the end        String pattern = "^"+ Regex.Escape(wildcard).Replace(@"\*", ".*").Replace(@"\?", ".") +"$";        // Now, run the Regex as you already know        Regex regex;        if(case_sensitive)            regex = new Regex(pattern);        else            regex = new Regex(pattern, RegexOptions.IgnoreCase);        return(regex.IsMatch(s));    } 

Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>