Click here to Skip to main content
12,505,592 members (54,378 online)
Rate this:
 
Please Sign up or sign in to vote.
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 12-Sep-12 6:08am
pucx1.2K
Comments
Kenneth Haugland 12-Sep-12 12:15pm
   
For that I would use Regular Expressions.
Wes Aday 12-Sep-12 12:16pm
   
Yep sounds like a regular expression to me too.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Look at the following tip (do not consider it an article, although so classified): Converting Wildcards to Regexes[^]
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Learn regexp:

The 30 Minute Regex Tutorial[^]

Now do some string replacement on regex like * with w+ etc
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160927.1 | Last Updated 15 Sep 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100