Wildcard Text Search

Anyone got a good fast wildcard (? and *) search routine for a text file?
davegreenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JimBob091197Commented:
Hi

This should do the trick:

function MatchStrings(Source, Pattern: String): Boolean;
var
      pSource: Array [0..255] of Char;
      pPattern: Array [0..255] of Char;
      function MatchPattern(element, pattern: PChar): Boolean;
            function IsPatternWild(pattern: PChar): Boolean;
            begin
                  Result := StrScan(pattern,'*') <> nil;
                  if not Result then Result := StrScan(pattern,'?') <> nil;
            end;
  begin
    if 0 = StrComp(pattern,'*') then
      Result := True
    else if (element^ = Chr(0)) and (pattern^ <> Chr(0)) then
      Result := False
    else if element^ = Chr(0) then
      Result := True
            else begin
      case pattern^ of
      '*': if MatchPattern(element,@pattern[1]) then
             Result := True
           else
             Result := MatchPattern(@element[1],pattern);
      '?': Result := MatchPattern(@element[1],@pattern[1]);
      else
                        if element^ = pattern^ then
          Result := MatchPattern(@element[1],@pattern[1])
        else
          Result := False;
      end;
    end;
      end;
begin
      StrPCopy(pSource, source);
      StrPCopy(pPattern, pattern);
      Result := MatchPattern(pSource,pPattern);
end;


Cheers,
JB
0
davegreenAuthor Commented:
Wow! the enter key is still warm from typing my question and I get an answer... excellent JimBob. This looks a good answer but is there a routine that will return the position in the string. I have a 2mb text string and I want to search for multiple occurances in it.. any ideas?
0
JimBob091197Commented:
(Sorry about the delay - my company's internet link went down!!)

My routine only does absolute matches, i.e. a match was found or not found.  The routine would have to be changed quite a lot to return a char pos.  Consider the following:

"This is a test."
If you search for "is" my routine will return False.  (My routine will only return True if you search for "*is*".)

Thus, the routine would have to change to return True for "is", and then it would be quite easy to get it to return the position of "is", "t?st", "t*t", etc.

If you don't get any other replies in the next few hours, I will look into doing this for you.  Unfortunately, I can't do it right this minute...

Cheers,
JB
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

ZifNabCommented:
Mmm, JB, ..... hehe....Is your real name David S.?
0
JimBob091197Commented:
Hi Zif,  my real name is David (not S).  Why do you ask?

0
ZifNabCommented:
Well, I found the same code on one of my examples directories and this was proposed by  David Stidolph

Ok, why making tools when there already exist some good ones? this one is freeware if you don't want the source code :

http://www.mindspring.com/~efd/tools.htm

Another is : torry.rimini.com/vcl/text/yg603w95.zip

But, if you still want to make your own... I'm willing to offer my help...

Regards, Zif.
0
JimBob091197Commented:
Well...  ;-)
I have a used this MatchStrings many times, and it's faster than the one I wrote myself.  In fact, faster than any others I have found.

JB
0
julio011597Commented:
Hello guys,

maybe i'm missing the point, but what about a simple StrPos() followed - in case of *s and ?s - by the needed checks on preceding and following chars? (i mean a check for blanks)

-julio
0
JimBob091197Commented:
Well, you may be on to something there, but you should write a function to check out the feasibility.

Cheers,
JB
0
davegreenAuthor Commented:
Thanks guys for all your input and suggestions... I'm going to have a look at hyperstring as ZifNab suggested. JimBob, I would be interested in looking at your modified routine should you have time to do it... Is julios suggestion a good one? would it be fast?  say for example I have a 2mb text file and I want to search for multiple occurances of *e?p?* and return the position in the text file of each one...
Your help is appreciated... thanks
Dave Green
0
davegreenAuthor Commented:
On reflection I didn't give a good example... change that to *e?p?m*    ? and * together isn't really valid although it shouldn't screw up the algorithm if it is entered..

Dave Green

0
julio011597Commented:
No, my suggestion is not that good; that is, i just thought about wildcards before and after the pattern!... sorry.

For real pattern matching all i can do is elaborate something from Sedgewick, which is not that RAD :)

-julio
0
JimBob091197Commented:
Hi Dave Green (& others)

The routine I gave to you is quick & efficient for small strings, but it uses recursion.  This means that for long strings and patterns, the nested procedure (MatchPattern) gets recursively called many times!!

Consider the following:
If you call "MatchStrings('one two three', '*two*')" then stepping through the code shows that MatchPattern at one point is 9 (!!) levels deep on the call stack!!  So you can just imagine what happens with a 2MB string and a longer pattern...  Stack overflows are bound to occur with long strings like you are talking of.

So I've decided that the routine isn't all that suitable after all, even if it were changed to return the string pos (as discussed in a previous comment).

Will continue to give it thought,
JB
0
davegreenAuthor Commented:
The Hyperstring Library looks good... so good I've just sent off for the source code.. ZifNab, the points are yours.. but how do I give them to you?

Dave Green
0
ZifNabCommented:
Hi davegreen,

Just accept this answer I'm giving you now....

Hyperstring Libraries are very good for this purpose (proven by DaveGreen, himself) ;-)):

       http://www.mindspring.com/~efd/tools.htm

Cheers ZiF.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davegreenAuthor Commented:
Thanks ZifNab
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.