Wildcard matching

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
dani.user
Posts: 284
Joined: Sep 30, 2006 10:41

Wildcard matching

Postby dani.user » Jun 09, 2007 20:18

I have written a function to match a string to a wildcard. It recognizes * or ?
Returns 0 or 1
You can use it in any project
No warranty

Code: Select all

declare function split(Expression as String, Delimiter As string, crar() as string) as integer

Function WildCardMatch(pattern1 as string, str1 as string) as byte
    Dim CrSP() as String
    Dim CrPos as Integer,CrPos2 as Integer
    Dim as Integer Fs
    Dim as String TempS
    Dim as Byte Add1 = 0
   
    dim as double hmm1,hmm2
    split pattern1,"*",CrSP()   
   
    CrPos = 1
   
    if UBound(CrSP) = 0 then
        if len(pattern1) <> len(str1) then
            return 0
        end if
        For iii as integer = 1 to len(pattern1)
            If Mid(pattern1,iii,1) <> "?" then
                If Mid(str1,iii,1) <> Mid(pattern1,iii,1) then
                    return 0
                End If
            End If
        Next 
        return 1
    end if
    For ii as Integer = 0 to Ubound(CrSP)
        CrPos2 = 0
        Fs = 0       
        If CrsP(ii) = "" then
            If Add1 <> 0 then
                CrPos += 1
            End If
        Else
            Add1 = 1
            If instr(CrSP(ii),"?") = 0 then
                CrPos2 = Instr(CrPos,str1,CrSP(ii))
                If CrPos2 = 0 Then
                    return 0           
                End If
                CrPos += len(CrSP(ii))
            Else
                For iii as integer = 1 to len(CrSP(ii))
                    If mid(CrSP(ii),iii,1) <> "?" then
                        Fs = iii
                        Exit For
                    End If
                Next
                If Fs > 0 then
                    CrPos2 = Instr(Fs,CrSP(ii),"?")
                    if CrPos2 > 0 then
                        TempS = Mid(CrSP(ii),Fs,CrPos2-Fs)
                    Else
                        TempS = Mid(CrSP(ii),Fs)
                    End If
                    CrPos += (Fs-1)
                    CrPos2 = Instr(CrPos,str1,TempS)
                    If CrPos2 = 0 then
                        return 0
                    Else
                        TempS = Mid(str1,CrPos2-(Fs-1),len(CrSP(ii)))

                        For iii as integer = 1 to len(TempS)
                            If Mid(CrSP(ii),iii,1) <> "?" then
                                If Mid(TempS,iii,1) <> Mid(CrSP(ii),iii,1) then
                                    return 0
                                End If
                            End If
                        Next
                    End If
                    CrPos += len(CrSP(ii))
                Else
                    If len(str1) < (CrPos + Len(CrSP(ii))) then
                        return 0
                    Else
                        CrPos += len(CrSP(ii))                       
                    End If
                End If
            End If
        End If
    Next
    return 1
End Function

? wildcardmatch("127.0.0.*","127.0.0.1")
? wildcardmatch("127.0.0.*","127.0.1.1")
? wildcardmatch("127.0.??.*","127.0.1.1")
? wildcardmatch("127.0.??.*","127.0.11.1")

sleep


function split(Expression as String, Delimiter As string, crar() as string) as integer
    Dim crar() as string
    Dim as long crpos1, crpos2
   
    crpos2 = instr(expression, delimiter)
    if crpos2 = 0 then
        redim crar(0)
        crar(0) = expression
        exit function
    else
        redim crar(0)
        crpos1 = 1
        crar(0) = mid(expression,crpos1,crpos2-crpos1)
        crpos1 = crpos2 + len(delimiter)
    end If
   
    Do
       crpos2 = instr(crpos1,expression,delimiter)
         
        if crpos2 > 0 Then
           redim preserve crar(ubound(crar)+1)
            crar(ubound(crar)) = mid(expression,crpos1,crpos2-crpos1)
            crpos1 = crpos2 + len(delimiter)
        end if
   
    loop until crpos2 = 0   
 
    if crpos1 <= len(expression) then
        redim preserve crar(ubound(crar)+1)
        crar(ubound(crar)) = mid(expression,crpos1)
    end If
   
    if len(expression) = crpos1 - len(delimiter) then
        redim preserve crar(ubound(crar)+1)
        crar(ubound(crar)) = ""       
    end if
   
    return 1
end function


Edit: Small upgrade
Last edited by dani.user on Jun 10, 2007 18:52, edited 2 times in total.
Sisophon2001
Posts: 1704
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Postby Sisophon2001 » Jun 10, 2007 5:15

It passed all my tests :)

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest