Click here to Skip to main content
13,147,183 members (29,861 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

47.5K views
13 bookmarked
Posted 7 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

 
QuestionI broke it Pin
Roman Shchekin9-Oct-14 3:11
memberRoman Shchekin9-Oct-14 3:11 
AnswerRe: I broke it Pin
Ben Hanson8-Jul-16 9:36
memberBen Hanson8-Jul-16 9:36 
GeneralReason for my vote of 5 nice article. Pin
nikhi _singh21-Feb-12 1:40
membernikhi _singh21-Feb-12 1:40 
GeneralReason for my vote of 5 Nice Algorithm. I modified the sourc... Pin
Dirk Moshage18-Jan-12 5:34
memberDirk Moshage18-Jan-12 5:34 
Question[My vote of 1] my vote of 1: why not use standard regex? Pin
SledgeHammer019-Feb-10 9:28
memberSledgeHammer019-Feb-10 9:28 
AnswerRe: [My vote of 1] my vote of 1: why not use standard regex? Pin
PrzemekBenz9-Feb-10 10:07
memberPrzemekBenz9-Feb-10 10:07 
AnswerRe: [My vote of 1] my vote of 1: why not use standard regex? Pin
Michael Lee Yohe16-Feb-10 12:47
memberMichael Lee Yohe16-Feb-10 12:47 
AnswerRe: [My vote of 1] my vote of 1: why not use standard regex? Pin
PIEBALDconsult20-Feb-12 12:03
memberPIEBALDconsult20-Feb-12 12:03 

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
Web03 | 2.8.170915.1 | Last Updated 27 Jul 2017
Article Copyright 2010 by PrzemekBenz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid