Code : Tout sélectionner
;=======================================================================================================
#include <array.au3> ;<== Seulement pour _ArrayDisplay()
$result = StringWildCard("Mercredi 6 Novembre 2013","Mercredi*Nov?")
MsgBox(0, "", $result)
; $result = "Mercredi 6 Nove"
$result = StringWildCard("Mercredi 6 Novembre 2013 Mercredi 30 Octobre 2013", "Mercredi???", 0, 1, 2)
MsgBox(0, "", $result)
; $result = "Mercredi 30"
$result = StringWildCard("Mercredi 6 Novembre 2013 Mercredi 30 Octobre 2013","Mercredi???", 1, 1)
If IsArray($result) Then
_ArrayDisplay($result)
;$result[0] = "Mercredi 6 "
;$result[1] = "Mercredi 30"
Else
MsgBox(0, "", $result)
EndIf
;Escape special characters
$String = "*?Blabla n°56 *\ Blabla"
$Compare = "*?Blabla n°"
$result = StringWildCard($String, StringWildCardEscape($Compare) &"?? \*\\")
MsgBox(0, "", $result)
;result = "*?Blabla n°56 *\"
;=======================================================================================================
;Function StringWildCard() :
;Basic WildCard, using only "*" and "?" filters,
;"*" means any string even empty,
;"?" means exactly one character
;"\" escapes a special character : "\?" or "\*" or "\\"
;Use StringWildCardEscape() to escape all special characters into a string.
;
;Success :
; If $ReturnArray = 0 :
; Returns the substring wich matchs the wildcard at the given occurence (default 1).
; If $ReturnArray = 1 :
; Returns an array containing all substrings which match.
;Failure :
; Returns 0 and set @error if :
; 1- There is matches, but less than $Occurence (only when not $ReturnArray)
; 2- $Start > Stringlen($String)
;
;Parameters
;$String : The string to search into for matches with the wildcard expression.
;$WildCard : The wildcard expression to found in the string.
;$ReturnArray : 0 or 1. Return methode. Default is 0.
;$Start : The string position to start the match (starts at 1). Default is 1.
;$Occurence : If not $ReturnArray, the occurence of match to return. Default is 1.
;$CaseSensitive : 0 or 1. Enable/disable case sensitive comparison.
Func StringWildCard($String, $Wildcard, $ReturnArray = 0, $Start = 1, $Occurence = 1, $CaseSensitive = 0)
If $Start > StringLen($string) Then Return SetError(2,0,0)
$Wildcard = StringReplace($Wildcard, "{", "\{")
$Wildcard = StringReplace($Wildcard, "}", "\}")
$Wildcard = StringReplace($Wildcard, "]", "\]")
$Wildcard = StringReplace($Wildcard, "[", "\[")
$Wildcard = StringReplace($Wildcard, "(", "\(")
$Wildcard = StringReplace($Wildcard, ")", "\)")
$Wildcard = StringReplace($Wildcard, "+", "\+")
$Wildcard = StringReplace($Wildcard, ".", "\.")
$Wildcard = __ReplaceNotEscaped($Wildcard, "?", ".{1}")
$Wildcard = __ReplaceNotEscaped($Wildcard, "*", ".*")
If Not $CaseSensitive Then $Wildcard = "(?i)" & $Wildcard
$result = StringRegExp($String, $Wildcard, 3, $Start)
If IsArray($result) Then
If $ReturnArray Then
Return $result
Else
If $Occurence > UBound($result) Then Return SetError(1,0,0)
Return $result[$Occurence -1]
EndIf
EndIf
Return 0
EndFunc
;Function StringWildCardEscape() :
;Escape all special characters into a string. ("*", "?", "\")
;If your wildcard contains a string variable, you may use StringWildCardEscape() on this variable
Func StringWildcardEscape($String)
$String = StringReplace($String, "\", "\\")
$String = StringReplace($String, "*", "\*")
$String = StringReplace($String, "?", "\?")
Return $String
EndFunc
Func __ReplaceNotEscaped($String, $Expression, $Replacement)
$Split = stringsplit($String, $Expression, 1)
$String = ""
For $i = 1 To $Split[0] -1
If IsInt((__nbSlashesAtEnd($Split[$i]) /2)) Then ;If there is a pair number of "\" before Expression
$string &= $Split[$i] & $Replacement ;Replace concerned occurence
Else
$string &= $Split[$i] & $Expression ;Or let it be.
EndIf
Next
$string &= $Split[$i]
Return $String
EndFunc
Func __nbSlashesAtEnd($String)
$i = 0
While StringRight($string, 1) = "\"
$i += 1
$string = StringTrimRight($string, 1)
WEnd
Return $i
EndFunc