[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# Wildcard Text Search

Posted on 1998-05-05
Medium Priority
385 Views
Anyone got a good fast wildcard (? and *) search routine for a text file?
0
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
• 6
• 5
• 3
• +1

LVL 5

Expert Comment

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

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

ID: 1341681

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

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

LVL 5

Expert Comment

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

0

LVL 8

Expert Comment

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

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

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

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

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...
Dave Green
0

Author Comment

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

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

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

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

ZifNab earned 680 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

ID: 1341694
Thanks ZifNab
0

## Featured Post

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, thâ€¦
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have toâ€¦
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a qâ€¦
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. â€¦
###### Suggested Courses
Course of the Month12 days, 17 hours left to enroll