Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# String matching using wildcards

Posted on 2008-10-15
2
Medium Priority
?
9,676 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

782 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