Comparer une chaine a une autre avec des caracteres speciaux (style "coucou" = "cou*u" ?)

Contenu du snippet

Cette fonction renvoie True si la chaine 1 est équivalente a la chaine 2, en tenant compte des caracteres spéciaux "*" ou "?"

en clair, si on demande si "coucou" est equivalent a "cou?ou" ça repondra True
pareil pour "coucou" = "cou*u"

Source / Exemple :


Public Function phrase_ok(ByVal chaine1 As String, ByVal chaine2 As String) As Boolean
Dim table() As String
Dim table_inter() As String
Dim ii As Integer
Dim posarg As Integer
Dim posarg2 As Integer
Dim oldposarg As Integer
Dim donnee As String
Dim donnee_len As Integer
phrase_ok = True

If InStr(1, chaine2, "*") = 0 And InStr(1, chaine2, "?") = 0 Then
    'si pas de carac speciaux
    If InStr(1, UCase(chaine1), UCase(chaine2)) = 0 Then
        'c pas bon
        phrase_ok = False
    Else
        'c bon
        Exit Function
    End If
End If

'd'abort les etoile
oldposarg = 0
table = Split(chaine2, "*")
If UBound(table) = 0 Then GoTo pasetoile
For ii = 0 To UBound(table)
    If table(ii) = "" Then Exit For
    table_inter = Split(table(ii), "?")
    If UBound(table_inter) > 0 Then
        'y'a des ?
        donnee = table_inter(UBound(table_inter))
    Else
        donnee = table(ii)
    End If
    posarg = InStr(oldposarg + 1, UCase(chaine1), UCase(donnee))
    If posarg > 0 Then
        If posarg < oldposarg Then
            'c pas bon
            phrase_ok = False
            Exit Function
        End If
        oldposarg = posarg
    Else
        'c pas bon
        phrase_ok = False
        Exit Function
    End If
Next ii

pasetoile:
oldposarg = -1
'puis les ?
table = Split(chaine2, "?")
For ii = 0 To UBound(table)
    If table(ii) = "" Then Exit For
    table_inter = Split(table(ii), "*")
    If UBound(table_inter) > 0 Then
        'y'a des *
        donnee = table_inter(UBound(table_inter))
    Else
        donnee = table(ii)
    End If
    donnee_len = Len(donnee)
    posarg = InStr(1, UCase(chaine1), UCase(donnee))
    'si on é pas au bout du tablo
    If ii < UBound(table) Then
        table_inter = Split(table(ii + 1), "*")
        If UBound(table_inter) > 0 Then
            'y'a des *
            donnee = table_inter(0)
        Else
            donnee = table(ii + 1)
        End If
        posarg2 = InStr(posarg + 1, UCase(chaine1), UCase(donnee))
    Else
        'sinon y é ce quer tout é bon
        Exit Function
    End If
    If posarg > 0 And posarg2 > 0 Then
        If posarg < oldposarg Then
            'c pas bon
            phrase_ok = False
            Exit Function
        ElseIf posarg + donnee_len = posarg2 - 1 Then
            'c bon
        Else
            'c pas bon
            phrase_ok = False
            Exit Function
        End If
        oldposarg = posarg
    Else
        'c pas bon
        phrase_ok = False
        Exit Function
    End If
Next ii

End Function

Conclusion :


Voila...
si ça se trouve y'a deja une fonction VB qui fait ça mais commeje la connais pas ;)

J'espere que ça servira a quelqu'un ;)

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.