WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

String Manipulation

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Wildcard UDF

Keywords: 	 wildcard UDF

I wanted a function ... WildCompare ... that would compare a filename (or whatever) to a string with wild-cards and tell me if it matched.

As long as I only wanted * and ? as wildcard characters, this works...

;-------------------------------------

#DefineFunction(Wild,Text)
return StrSubWild(Text,Wild) == Text
#EndFunction

;-------------------------------------
However, I needed something else. I was looking for files who's names had a numeric character in certain position. I really wanted to use # as a wildcard character meaning "Any one numeric character".

Here's the routine that I wrote to do it. Note that it could easily be adapted to any other special need. (For example, you wanted to match "Any Alphatabetic character")


;************************************************************************************
; Compare a Text String to a wildcard. Return @TRUE if it matches.
;
; This routine matches using the following wildcards...
;
; * ... zero or more of any characters at this point 
; ? ... one of any character at this point.
; # ... one numeric character at this point.
;
;************************************************************************************
#DefineFunction WildCompare(Wild, Txt) 
if (Wild == "") then return @TRUE ; Empty wildcard = TRUE
if (Wild == "*") then return @TRUE ; "*" matches anything
while (1) 
if (Wild == "") ; if end of wildcard is also 
if (Txt == "")then return @TRUE ; end of Txt, we match.
return @FALSE;
endif

Cw = StrSub(Wild,1,1) ; Character of wildcard
if (Cw == '*') ; Is it *?
Wild = StrSub(Wild,2,-1) ; Bump wildcard to next position.
while (1) ;See if we can match the rest
if (WildCompare(Wild, Txt))
Return @TRUE 
endif
if (Txt == "")
Return @FALSE 
endif
Txt = StrSub(Txt,2,-1) 
endwhile
endif

if (Cw == '?') ; A question mark matches anything
if (Txt == "") Then Return @FALSE;
Txt = StrSub(Txt,2,-1)
Wild = StrSub(Wild,2,-1)
continue;
endif

if (Cw == '#') ; A # matches any numeric character
Ctx = StrSub(Txt,1,1)
if (Ctx < "0" || Ctx > "9") Then Return @FALSE;
Txt = StrSub(Txt,2,-1)
Wild = StrSub(Wild,2,-1)
continue;
endif

if (StrSub(Txt,1,1) != Cw) Then Return @FALSE;
Wild = StrSub(Wild,2,-1)
Txt = StrSub(Txt,2,-1)
endwhile 
#EndFunction 



Article ID:   W14999
File Created: 2001:11:08:12:41:20
Last Updated: 2001:11:08:12:41:20