Click here to Skip to main content
13,254,154 members (61,134 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

48.4K views
13 bookmarked
Posted 8 Feb 2010

Use wildcard Characters * and ? to Compare Strings

, 26 Jul 2017
Rate this:
Please Sign up or sign in to vote.
How to use wildcard characters & and ? to compare strings

Method to compare Strings with wildcard characters:

The following wildcards can be used in patterns:

  • '?' - any single character
  • '*' - zero or more 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("*?", "a"));
Assert.IsTrue(MatchWildcardString("?*", "a"));
Assert.IsTrue(MatchWildcardString("*?*", "a"));
Assert.IsTrue(MatchWildcardString("*", "ab"));
Assert.IsTrue(MatchWildcardString("**", "ab"));
Assert.IsTrue(MatchWildcardString("***", "ab"));
Assert.IsTrue(MatchWildcardString("*b", "ab"));
Assert.IsTrue(MatchWildcardString("a*", "ab"));
Assert.IsTrue(MatchWildcardString("?", "a"));
Assert.IsTrue(MatchWildcardString("*?", "abc"));
Assert.IsTrue(MatchWildcardString("*??", "abc"));
Assert.IsTrue(MatchWildcardString("??*", "abc"));
Assert.IsTrue(MatchWildcardString("?*?", "abc"));
Assert.IsTrue(MatchWildcardString("???", "abc"));
Assert.IsTrue(MatchWildcardString("?*", "abc"));
Assert.IsTrue(MatchWildcardString("*abc", "abc"));
Assert.IsTrue(MatchWildcardString("*abc*", "abc"));
Assert.IsTrue(MatchWildcardString("*a*b*c*", "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?b", "ab"));
Assert.IsFalse(MatchWildcardString("abc", "ab"));
Assert.IsFalse(MatchWildcardString("ab", "cab"));
Assert.IsFalse(MatchWildcardString("ab", "abc"));
Assert.IsFalse(MatchWildcardString("ab", "AB"));
Assert.IsFalse(MatchWildcardString("AB", "ab"));
Assert.IsFalse(MatchWildcardString("cab", "ab"));
Assert.IsFalse(MatchWildcardString("??", "a"));
Assert.IsFalse(MatchWildcardString("*?", ""));
Assert.IsFalse(MatchWildcardString("?*", ""));
Assert.IsFalse(MatchWildcardString("????", "abc"));
Assert.IsFalse(MatchWildcardString("??*", "a"));
Assert.IsFalse(MatchWildcardString("*abc", "abX"));
Assert.IsFalse(MatchWildcardString("*abc*", "Xbc"));
Assert.IsFalse(MatchWildcardString("*a*bc*", "ac"));
//Performance Tests
String matchSubString = "ababac";
String inputABmC = (new String('A', 10000)) + 
(new String('B', 10000)) + matchSubString + (new String('C', 10000));
String inputABm = (new String('A', 10000)) + (new String('B', 10000)) + matchSubString;
String inputmBC = matchSubString + (new String('B', 10000)) + (new String('C', 10000));

Assert.IsFalse(MatchWildcardString(matchSubString, inputABmC));
Assert.IsFalse(MatchWildcardString(matchSubString + "*", inputABmC));
Assert.IsFalse(MatchWildcardString("*" + matchSubString, inputABmC));
Assert.IsTrue(MatchWildcardString("*" + matchSubString + "*", inputABmC));
Assert.IsTrue(MatchWildcardString("*" + matchSubString, inputABm));
Assert.IsTrue(MatchWildcardString(matchSubString + "*", inputmBC));
Assert.IsTrue(MatchWildcardString(inputABmC, inputABmC));

I tested MatchWildcardString method according to Roman Shchekin suggestion with long input text 4k and I was able to reproduce error: "Stack overflow exception". This is because there is a memory limit in the .NET Framework in case of recurrence execution. If you need to use MatchWildcardString for longer text than 3k, please use the following implementation (which will give you the same result without error exception):

private Boolean MatchWildcardString(String pattern, String input)
{
    String regexPattern = "^";

    foreach (Char c in pattern)
    {
        switch (c)
        {
            case '*':
                regexPattern += ".*";
                break;
            case '?':
                regexPattern += ".";
                break;
            default:
                regexPattern += "[" + c + "]";
                break;
        }
    }
    return new Regex(regexPattern + "$").IsMatch(input);
}

License

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

Share

About the Author

PrzemekBenz
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralReason for my vote of 5 nice article. Pin
nikhi _singh21-Feb-12 2:40
membernikhi _singh21-Feb-12 2:40 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 27 Jul 2017
Article Copyright 2010 by PrzemekBenz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid