Solved

C# String matching using wildcards

Posted on 2008-10-15
2
9,594 Views
Last Modified: 2012-05-05
Hey,

How do i implement a wildcard matching method in c# using e.g regex ?

It should work like this:
String expr1 = "*test*" meaning that its a match if "test" is anywhere in the string
String expr2 = "*test" meaning that its a match if "test" is in the end of the string
String expr3 = "test*" meaning that its a match if "test" is in the beggning of the string

Example:
String text1 = "This is a test"
String test2 = "This test is mine"
String test3 = "testing is nice"

expr1 will match test1, test2 and test3
expr2 will match text1
expr3 will match text3

Thanks
0
Comment
Question by:ravedome
2 Comments
 

Accepted Solution

by:
ravedome earned 0 total points
ID: 22719439
This can be closed, I found a solution by myself.
0
 
LVL 9

Expert Comment

by:ripahoratiu
ID: 22719470
       public static bool Match(string strWithWildCards, string myString)
        {
            if (strWithWildCards.Length==0) return myString.Length==0;
            if (myString.Length==0) return false;
            if (strWithWildCards[0] == '*' && strWithWildCards.Length > 1)
                for (int index = 0; index < myString.Length; index++)
                {
                    if (Match(strWithWildCards.Substring(1), myString.Substring(index)))
                        return true;
                }
             else if (strWithWildCards[0] == '*')
                        return true;
             else if (strWithWildCards[0] == myString[0])
                            return Match(strWithWildCards.Substring(1), myString.Substring(1));
            return false;
            }
        }
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now