Re: Wildcard String Compare
Posted: Tue May 31, 2011 8:44 pm
so... my searchfu must have been pretty weak before I had written my own function...but now that I am DONE I found this thread! : http://www.purebasic.fr/english/viewtop ... g+wildcard
bah... lots of good solutions in here, but here is my implementation of a FindString with Wildcard. so far it has worked for my test cases.
Now I'll have to go back and review the others.
bah... lots of good solutions in here, but here is my implementation of a FindString with Wildcard. so far it has worked for my test cases.
Now I'll have to go back and review the others.
Code: Select all
Procedure.i explodeArray(Array sArr.s(1), string.s, separator.s = ",", trimString.i = #False, limit.i = 0) ; String to Array
;From Flype's Functions - http://www.purebasic.fr/english/viewtopic.php?f=12&t=21495&start=0
;takes a list of delimited string and explodes out into an array
Protected.i index, size = CountString(string, separator)
If Len(separator) > 1
ProcedureReturn 0
EndIf
If (limit > 0)
size = limit - 1
ElseIf (limit < 0)
size + limit
EndIf
ReDim sArr.s(size)
For index = 0 To size
If trimString
sArr(index) = Trim(StringField(string, index + 1, separator))
Else
sArr(index) = StringField(string, index + 1, separator)
EndIf
Next index
ProcedureReturn size
EndProcedure
Procedure.i FindStringWC(String.s,StringToFind.s, wildcard.s = "*", caseSensitive.i=#False)
;tests to see if a string is found, and supports single or multiple wildcard use.
; Returns 0 or 1
; String.s is the string to search
; StringToFind is the pattern to search for
; ie.
; Debug "these should be true: "
; Debug FindStringWC("this is a test","this is a test","*") ; true
; Debug FindStringWC("this is a test","this is *","*") ; true
; Debug FindStringWC("this is still a test","this is *","*") ; true
; Debug FindStringWC("this is a test","*a test","*") ; True
; Debug FindStringWC("this is a test","this * test","*") ; true
; Debug FindStringWC("blahblahblah this is a test blahblah","*this is a test*") ; true
; Debug FindStringWC("blahblahblah this is a test blahblah","*this is a test*") ; true
; Debug FindStringWC("this is a test","this * test") ; true
; Debug FindStringWC("this test is true","this test is *") ; true
; Debug FindStringWC("true this test is","* this test is") ; true
; Debug "these should all be false:"
; Debug FindStringWC("this is a test and should be false","this is test","*") ; f
; Debug FindStringWC("blahblahblah this is a false test blahblah","*this is a test*") ; f
; Debug FindStringWC("this test is false","this * test") ; f
; Debug FindStringWC("not this test","this test is *") ; f
; Debug FindStringWC("this test is false","* this test is") ; f
; Debug FindStringWC("blahblahblah this is a false test blahblah","*this is a test*") ; f
; Debug FindStringWC("this is a False","this * test","*") ; f
; Debug FindStringWC("this is a test","a test*","*") ; f
If Not caseSensitive
string = UCase(String)
StringToFind = UCase(StringToFind)
EndIf
Protected.i AnyPos = -1 ;wild
Protected.i i, j, ub
Protected Dim sSection.s(0)
explodearray(sSection(),StringToFind,wildcard,#False)
ub = ArraySize(sSection())
Protected Dim iPos.i(ub)
Protected.i nStart = AnyPos
For i = 0 To ub
If sSection(i) = ""
iPos(i) = AnyPos ;wild
Else
If i > 0
nStart = iPos(i-1)
EndIf
If nStart <=0
nStart = 1
EndIf
iPos(i) = FindString(String,sSection(i),nStart)
If iPos(i) = 0 ;not found
ProcedureReturn #False
ElseIf i = 0 And sSection(i) <> "" And iPos(i) <> 1 ;verify the left side
;left side not wild, but string2find not at beginning
ProcedureReturn #False
EndIf
EndIf
Next i
;now verify the right side:
If sSection(ub) <> "" ;right side not wild
If iPos(ub) <> Len(String)-Len(sSection(ub)) + 1
ProcedureReturn #False
EndIf
EndIf
ProcedureReturn 1
EndProcedure
Debug "these should be true: "
Debug FindStringWC("this is a test","this is a test","*") ; true
Debug FindStringWC("this is a test","this is *","*") ; true
Debug FindStringWC("this is still a test","this is *","*") ; true
Debug FindStringWC("this is a test","*a test","*") ; True
Debug FindStringWC("this is a test","this * test","*") ; true
Debug FindStringWC("blahblahblah this is a test blahblah","*this is a test*") ; true
Debug FindStringWC("blahblahblah this is a test blahblah","*this is a test*") ; true
Debug FindStringWC("this is a test","this * test") ; true
Debug FindStringWC("this test is true","this test is *") ; true
Debug FindStringWC("true this test is","* this test is") ; true
Debug "these should all be false:"
Debug FindStringWC("this is a test and should be false","this is test","*") ; f
Debug FindStringWC("blahblahblah this is a false test blahblah","*this is a test*") ; f
Debug FindStringWC("this test is false","this * test") ; f
Debug FindStringWC("not this test","this test is *") ; f
Debug FindStringWC("this test is false","* this test is") ; f
Debug FindStringWC("blahblahblah this is a false test blahblah","*this is a test*") ; f
Debug FindStringWC("this is a False","this * test","*") ; f
Debug FindStringWC("this is a test","a test*","*") ; f
End