Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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.
Posted
Comments
Kenneth Haugland 12-Sep-12 12:15pm    
For that I would use Regular Expressions.
[no name] 12-Sep-12 12:16pm    
Yep sounds like a regular expression to me too.

Look at the following tip (do not consider it an article, although so classified): Converting Wildcards to Regexes[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Sep-12 20:28pm    
Interesting article, to the matter of this question. Hope it also works... :-)
Well, my 5.
--SA
Learn regexp:

The 30 Minute Regex Tutorial[^]

Now do some string replacement on regex like * with w+ etc
 
Share this answer
 
try below code

C#
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
}
 
Share this answer
 
Comments
Andreas Gieriet 15-Sep-13 7:18am    
This is not robust. Use Regex.Escape to do the proper transformation.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900