Solved

C# String matching using wildcards

Posted on 2008-10-15
2
9,631 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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