Solved

C# String matching using wildcards

Posted on 2008-10-15
2
9,621 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

Industry Leaders: 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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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