Solved

Wildcard Text Search

Posted on 1998-05-05
16
383 Views
Last Modified: 2010-08-05
Anyone got a good fast wildcard (? and *) search routine for a text file?
0
Comment
Question by:davegreen
[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
  • 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
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!

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

726 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