Click here to Skip to main content

Use wildcard characters * and ? to compare strings

Method to compare Strings with wildcard characters:public Boolean MatchWildcardString(String pattern, String input){ if (String.Compare(pattern, input) == 0) { return true; } else if(String.IsNullOrEmpty(input)) { if...
Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment
See more: C#
Method to compare Strings with wildcard characters:
 
public Boolean MatchWildcardString(String pattern, String input)
{
    if (String.Compare(pattern, input) == 0)
    {
        return true;
    }
    else if(String.IsNullOrEmpty(input))
    {
        if (String.IsNullOrEmpty(pattern.Trim(new Char[1] { '*' })))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else if(pattern.Length == 0)
    {
        return false;
    }
    else if (pattern[0] == '?')
    {
        return MatchWildcardString(pattern.Substring(1), input.Substring(1));
    }
    else if (pattern[pattern.Length - 1] == '?')
    {
        return MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input.Substring(0, input.Length - 1));
    }
    else if (pattern[0] == '*')
    {
        if (MatchWildcardString(pattern.Substring(1), input))
        {
            return true;
        }
        else
        {
            return MatchWildcardString(pattern, input.Substring(1));
        }
    }
    else if (pattern[pattern.Length - 1] == '*')
    {
        if (MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input))
        {
            return true;
        }
        else
        {
            return MatchWildcardString(pattern, input.Substring(0, input.Length - 1));
        }
    }
    else if (pattern[0] == input[0])
    {
        return MatchWildcardString(pattern.Substring(1), input.Substring(1));
    }
    return false;
}
 
The above method can be tested in the following way:
 
// Positive Tests
Assert.IsTrue(MatchWildcardString("*", ""));
Assert.IsTrue(MatchWildcardString("?", " "));
Assert.IsTrue(MatchWildcardString("*", "a"));
Assert.IsTrue(MatchWildcardString("*", "ab"));
Assert.IsTrue(MatchWildcardString("?", "a"));
Assert.IsTrue(MatchWildcardString("*?", "abc"));
Assert.IsTrue(MatchWildcardString("?*", "abc"));
Assert.IsTrue(MatchWildcardString("*abc", "abc"));
Assert.IsTrue(MatchWildcardString("*abc*", "abc"));
Assert.IsTrue(MatchWildcardString("*a*bc*", "aXXXbc"));
 
// Negative Tests
Assert.IsFalse(MatchWildcardString("*a", ""));
Assert.IsFalse(MatchWildcardString("a*", ""));
Assert.IsFalse(MatchWildcardString("?", ""));
Assert.IsFalse(MatchWildcardString("*b*", "a"));
Assert.IsFalse(MatchWildcardString("b*a", "ab"));
Assert.IsFalse(MatchWildcardString("??", "a"));
Assert.IsFalse(MatchWildcardString("*?", ""));
Assert.IsFalse(MatchWildcardString("??*", "a"));
Assert.IsFalse(MatchWildcardString("*abc", "abX"));
Assert.IsFalse(MatchWildcardString("*abc*", "Xbc"));
Assert.IsFalse(MatchWildcardString("*a*bc*", "ac"));
 
Best Regards,
Przemyslaw Benz
Posted 8 Feb '10
Edited 6 May '10

Comments
Dirk Moshage - 18 Jan '12
Reason for my vote of 5 Nice Algorithm. I modified the source code in c with pointer and length so there ist no problem with the heap.

Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Alternative 1

Using the Substring operation repeatedly as you do is apt to be very slow. Better would be to pass the starting indices and lengths of the two strings as parameters. That would require a manual loop to replace the String.Trim function, but it would avoid the need to create lots of new string objects on the heap. For short strings it wouldn't matter too much, but I would expect your routine would get really slow if the strings are a few thousand characters long.
  Permalink  

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

Your Filters
Interested
Ignored
     
  1. Christian Graus (1,194)
  2. SAKryukov (620)
  3. CRDave1988 (279)
  1. SAKryukov (10,679)
  2. Christian Graus (7,188)
  3. OriginalGriff (5,239)
  4. Abhinav S (4,575)
  5. thatraja (4,466)

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Question[My vote of 1] my vote of 1: why not use standard regex? PinmemberSledgeHammer0110:28 9 Feb '10  
AnswerRe: [My vote of 1] my vote of 1: why not use standard regex? PinmemberPrzemekBenz11:07 9 Feb '10  
AnswerRe: [My vote of 1] my vote of 1: why not use standard regex? PinmemberMichael Lee Yohe13:47 16 Feb '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 6 May 2010
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid