Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# String matching using wildcards

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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