Click here to Skip to main content
Click here to Skip to main content

Wildcard Manipulation for Text

By , 18 May 2005
 

Sample Image - diskinfo.png

Introduction

Sometimes it is handy to have an utility that will search for a certain text pattern or execute a find and replace based on it. The string provides some limited ability to do that, but you are out of luck if you need these functions to be case insensitive and to use a wildcard algorithm.

Background

The tradeoff for this situation is that, you cannot look for ‘*’ and ‘?’ in your target text. But that’s what the string is good for, isn’t it!

I’ve included a solution with two projects. The DLL has the code that implements algorithm and the executable is merely a client that would let you enjoy the functionality in a visual way. Here is the code that's the heart for this algorithm.

protected bool _PatternMatched(int IndexText, int IndexPattern)
{            
  while (true)
  {
    char CrtTxtChar, CrtPatternChar;
    CrtPatternChar= 
      (IndexPattern == patternlen)?'*':ConvertCase(strpattern[IndexPattern++]);
    switch (CrtPatternChar )
    {
      case '*':
        if(IndexPattern == patternlen)//last *, used by FindPatternPosition
        {
          this.last = IndexText;
          return true;// last pattern char == *, no need to check the last chars
        }
        while (IndexText < txtlen)
        {               
          if(_PatternMatched(IndexText++,IndexPattern))
          {
            return true;
          }
        }
        return false;
        break;
      case '?':
        if(IndexText == txtlen)
          return false;
        IndexText++;
        break;
      default:
        if(IndexText == txtlen)
          CrtTxtChar = '\0';
        else
          CrtTxtChar = ConvertCase( strtxt[IndexText++] );
        if( CrtTxtChar != CrtPatternChar )  // check for exact char
        {
          if(CrtTxtChar=='\0')
            return false;
          else if(_PatternMatched(IndexText, 0))
          {
            if(this.first < IndexText)
              this.first = IndexText;
            return true;
          }
          else
            return false;
        }
        else
        {
          if(this.last < IndexText -1)
            this.last = IndexText -1;
        }
        break;
    }//switch ends
  }//while ends
}//_PatternMatched ends

This protected method is used by the public methods IsPatternMatch and ReplaceTextEx that are exposed by this class. The first and last data members store the position of the pattern in the target string in order to allow the replace to work.

Using the code

Just instantiate a class of WildCardTxtUtility and call its public functions.

Beware

A large target text and multiple wildcards in the pattern will cause performance issues. I would welcome any attempt to address them.

History

  • This is version 1.0.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

dmihailescu
Software Developer (Senior)
United States United States
Member
Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRegular ExpressionsmemberEric Woodruff18 May '05 - 16:22 
GeneralRe: Regular ExpressionssitebuilderUwe Keim18 May '05 - 18:39 
GeneralI did not know about themmemberdmihailescu19 May '05 - 2:57 
GeneralRe: Regular Expressionsmemberzwir31 Jul '07 - 22:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 18 May 2005
Article Copyright 2005 by dmihailescu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid