Solved

Wildcard Text Search

Posted on 1998-05-05
16
375 Views
Last Modified: 2010-08-05
Anyone got a good fast wildcard (? and *) search routine for a text file?
0
Comment
Question by:davegreen
  • 6
  • 5
  • 3
  • +1
16 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1341679
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
 

Author Comment

by:davegreen
ID: 1341680
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
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1341681
(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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341682
Mmm, JB, ..... hehe....Is your real name David S.?
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1341683
Hi Zif,  my real name is David (not S).  Why do you ask?

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341684
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
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1341685
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
 
LVL 5

Expert Comment

by:julio011597
ID: 1341686
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Expert Comment

by:JimBob091197
ID: 1341687
Well, you may be on to something there, but you should write a function to check out the feasibility.

Cheers,
JB
0
 

Author Comment

by:davegreen
ID: 1341688
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
 

Author Comment

by:davegreen
ID: 1341689
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
 
LVL 5

Expert Comment

by:julio011597
ID: 1341690
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
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1341691
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
 

Author Comment

by:davegreen
ID: 1341692
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
 
LVL 8

Accepted Solution

by:
ZifNab earned 170 total points
ID: 1341693
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
 

Author Comment

by:davegreen
ID: 1341694
Thanks ZifNab
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now